Auto-Create Page associated Widgets
July 16, 2009So here’s the idea: I’ve had many clients in the past who wanted to have page-specific widgetized sidebars. Now, I’ve covered this before, but one thing has always gotten me: the fact that you have to write a lot of bloated code to get it to work. It seems like you should be able to pull the post slugs into some kind of array, and have the widgets auto-create themselves with a small bit of code, rather than adding more code every time you add a new page. I’ve com really close to pulling this off in the past, but – as the saying goes – no cigar.
Until now ![]()
My initial code was the following:
$pages = get_pages();
$num = count($pages);
if ( function_exists('register_sidebar') ) {
register_sidebars($num, array(
'name'=> 'Sidebar %d',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
}
This is where I had partial success. The above would, indeed, create a new widget section for every single Page I’d created in my WordPress. The problem? each section was named “Sidebar [number]“. What I wanted was “Sidebar [pagename]” so I would know exactly which sidebar was associated with what Page. However, when I tried to edit the code to make that happen, I was stymied by a fatal error appearing on the front end of the site. The page names did, indeed show up as I wanted them to on the back end – but what’s the point if the front end doesn’t work? So it looked like the code above was as close as I was going to get.
In steps jcow over at the WordPress forums. Now – keep in mind I haven’t completely tested this, but initial trials show lots of joy
Basically, the idea is to wrap my function into a function, like so:
$before_widget = '<div id="%1$s" class="widget %2$s">';
$after_widget = '</div>';
$before_title = '<h3>';
$after_title = '</h3>';
if (function_exists('register_sidebar'))
register_sidebar(array(
'name' => 'Home Sidebar',
'before_widget' => $before_widget,
'after_widget' => $after_widget,
'before_title' => $before_title,
'after_title' => $after_title
));
createPageSidebars();
function createPageSidebars() {
global $before_widget, $after_widget, $before_title, $after_title;
if ( function_exists('register_sidebar') ) {
$pages = get_pages();
foreach ($pages as $page) {
register_sidebar(array(
'name' => $page->post_title .' Sidebar',
'before_widget' => $before_widget,
'after_widget' => $after_widget,
'before_title' => $before_title,
'after_title' => $after_title));
}
}
}
Now my dream has come true (I know, slightly pathetic, right?): I now have auto-created widgetized sidebars in the back end, named properly and ready go to. But how do I use them to display the right stuff on the front end, without using all of those “if/else” statements? That part is still manual. Not so!
<div id="sidebar">
<?php // Dynamic Sidebars
if (is_home() && function_exists('dynamic_sidebar') && dynamic_sidebar('Home Sidebar')) :
elseif (function_exists('dynamic_sidebar') && dynamic_sidebar($wp_query->post->post_title.' Sidebar')) :
else : // If there is no dynamic sidebar for this page ?>
put in the default sidebar code here for the display of pages that have no widgetied sidebar association
<?php endif; ?>
</div>
Total awesomeness!
Now, for the “warning” part. One thing I’ve noticed about Widgetized sidebars is that if you rearrange them, then the wrong information gets pulled into the wrong page. For example, I had one client that I used the old method with, and when she deleted a Page, or removed (or moved) an “if/else” statement, then everything got REALLY screwed up and she had to redo her widgets all over again. Not really that bad – except for the text widgets, which meant she had to rewrite a bunch of stuff. So right now, I’m still testing what happens if you use this method and add or remove Pages. So far, for the initial creation of a site, this works a treat. But if you add or remove Pages, it could mess things up. I haven’t checked that part yet – and I’m going to do that later today and will report back. But in the meantime, if you try this, be sure to back up your database and files (or record what your widgets are so you can manually revert them if this doesn’t work) before you try this.
I will come back and tell you how it went. But it was so cool to find out how to do it, I had to share immediately!
Update: it was, indeed, as I thought. You can auto-create, no problem. But deleting is another thing. If you delete a Page, then any Pages created after that, their sidebars will shift up a notch to replace the deleted one. Hmm…I think now we’ll have to figure out how to “unset” a sidebar when the Page is deleted so the shift doesn’t happen. I’ve also found that re-creating the Page (with the same name) will make the sidebars shift back into place as if nothing happened. unfortunately, saving the Page as “Draft” or “Pending Review” causes the sidebars to shift again. So I guess a little work is needed at the moment to perfect this!









P.O. Box 46
Sorry, comments are now closed on this post. You may thank the spammers for that one. But if you have any questions, please feel free to email me and ask - maybe it'll make for a good update in a future post. :)