by Chris Coyier & Jeff Starr

Declare Multiple Widgetized Areas

Posted by Updated on

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) {
           '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

  1. 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.

  2. 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. :)

  3. curtismchale March 18, 2010

    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.

  4. Okay, I’ll apologize up front for the noob question, but…would I add this script to index.php? Or sidebar.php? Or…? I’m leaning toward index.php but just not sure.

  5. Oooh…think I found it…functions.php? Now I just have to figure out how to edit widgets!

  6. Denis Leblanc March 28, 2010

    Thanks Chris, saved me tons of time.

  7. Josh Monteiro April 19, 2010

    Simply in love with all this.

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry