Declare Multiple Widgetized Areas in WordPress
Have a bunch of different areas you wish to declare as a widgetized area? Save repetative code by creating a quick array of their names, then loop through that array calling the register_sidebar() function on each one. Elementary PHP stuff here, but hey, it just saved me quite a few lines of code in a widget-heavy theme I am working on.
if ( function_exists('register_sidebar') ) {
$allWidgetizedAreas = array("Homepage Left", "Homepage Right", "Sidebar One", "Movies", "Admin");
foreach ($allWidgetizedAreas as $WidgetAreaName) {
register_sidebar(array(
'name'=> $WidgetAreaName,
'before_widget' => '<div id="%1$s" class="widget %2$s left half">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
}
}
7 responses
-
You know that is a very simple script but a great idea. Nice work. The result is not just saving you lines of code but saving you time which is important.
-
While this is pretty cool it doesn’t let you add descriptions to your widgetized areas (which we should all be doing). I personally don’t think it’s really functionally usable on a theme without being able to add descriptions to help the end user know what a sidebar is for.
-
I use this type of trick all the time. I’ve wrapped the loop into a function and tucked in to my standard wp lib. I’ve always wondered why more people don’t use a iterative approach in php/wp development.
Thanks for sharing. :)
-
Okay, I’ll apologize up front for the noob question, but…would I add this script to
index.php
? Orsidebar.php
? Or…? I’m leaning towardindex.php
but just not sure. -
Oooh…think I found it…
functions.php
? Now I just have to figure out how to edit widgets! -
Thanks Chris, saved me tons of time.
-
Simply in love with all this.