Collapsing WordPress Widget-Ready Areas & Sidebars

Problem: Theme users need as many widget-ready areas as possible. They’re easy to use and they help keep theme template files clean. But! unused, empty, widget-ready areas, areas waiting for you to add a widget of your choice, hang around waiting to spoil your designs by limiting your styling options. Just as bad, empty widget areas can invalidate your markup. But don’t worry, we can do something about it. Only first, we need to take a closer look at the problem.

Here’s how a typical widget-ready sidebar looks. I’ve taken the code from Automattic’s guide to widgetizing WordPress themes.

  1. <ul id="sidebar">
  2. <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
  3.  <!– likely awesome stuff –>
  4. <?php endif; ?>
  5. </ul>

We’ve got an unordered list and inside of it a statement that looks for the dynamic_sidebar() function. If we’ve registered one, it’ll spit out our awesome stuff: WordPress widgets marked up as list items.

All very good. But what if we start out with no awesome stuff in there? No default widgets? Well, that means our containing ul element will be hanging out there in our site with no li elements in it. A list without list items. Totally invalid and totally unsemantic. Worse yet: what if you were styling that ul with a margin, border, or just positioning it? That styling would stick—even if the widget area was empty. Totally unacceptable.

Thankfully, there’s a fix.

  1. // Check for widgets in widget-ready areas http://wordpress.org/support/topic/190184?replies=7#post-808787
  2. // Thanks to Chaos Kaizer http://blog.kaizeku.com/
  3. function is_sidebar_active( $index = 1){
  4.  $sidebars = wp_get_sidebars_widgets();
  5.  $key  = (string) 'sidebar-'.$index;
  6.  
  7.  return (isset($sidebars[$key]));
  8. }

Blogger Avice (Chaoskaizer) De’véreux came up with the nifty little function above, is_sidebar_active(), in response to a question in the WordPress support forums. It’s a beaut. With it you can check to see if there are widgets in a sidebar and if not, well, you can do something else. Like nothing for an example. Nothing, in this case, is really something.

Here’s an example of the code from the beginning of this post, modified to produce no widget-ready area by default and load one only if there is a widget added to it.

  1. <?php if ( function_exists('dynamic_sidebar') && is_sidebar_active() ) { // there are active widgets for this sidebar
  2.             echo '<ul id="sidebar">'. "\n";
  3.             dynamic_sidebar();
  4.             echo '</ul>' . "\n";
  5.         } ?>

Now, where would this be useful? Well, Thematic is going to start making use of it. With 13 widget-ready areas and only two containing widgets by default, it was in desperate need of this technique. Here’s how Thematic is using is_sidebar_active() in it’s footer to only create a containing div for a group of 3 widget-ready areas if a widget is loaded into any one of them.

  1. <?php if ( function_exists('dynamic_sidebar') ) { ?>
  2.     <?php if ( is_sidebar_active(3) || is_sidebar_active(4) || is_sidebar_active(5) ) { // one of the subsidiary asides has a widget ?>
  3.     <div id="subsidiary">
  4.    
  5.         <?php if ( function_exists('dynamic_sidebar') && is_sidebar_active(3) ) { // there are active widgets for this aside
  6.             echo '<div id="first" class="aside sub-aside">'. "\n" . '<ul class="xoxo">' . "\n";
  7.             dynamic_sidebar(3);
  8.             echo '</ul>' . "\n" . '</div><!– #first .aside –>'. "\n";
  9.         } ?>                
  10.    
  11.         <?php if ( function_exists('dynamic_sidebar') && is_sidebar_active(4) ) { // there are active widgets for this aside
  12.             echo '<div id="second" class="aside sub-aside">'. "\n" . '<ul class="xoxo">' . "\n";
  13.             dynamic_sidebar(4);
  14.             echo '</ul>' . "\n" . '</div><!– #second .aside –>'. "\n";
  15.         } ?>      
  16.    
  17.         <?php if ( function_exists('dynamic_sidebar') && is_sidebar_active(5) ) { // there are active widgets for this aside
  18.             echo '<div id="third" class="aside sub-aside">'. "\n" . '<ul class="xoxo">' . "\n";
  19.             dynamic_sidebar(5);
  20.             echo '</ul>' . "\n" . '</div><!– #third .aside –>'. "\n";
  21.         } ?>        
  22.        
  23.     </div><!– #subsidiary –>
  24.     <?php } ?>
  25. <?php } ?>

I’m a big fan of widget-ready areas. WordPress themes should be using them more creatively and empowering users to take control of their templates. This technique only makes things even easier. Try adapting it in your next WordPress theme and make something beautiful—and usable too.

Don't forget: You should follow me on twitter here.

Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted August 14, 2008 at 7:40 pm | Permalink

    Ian, this is great. I’m using it for a new theme I’m creating, and it works wonderfully. Thanks for seeking it out!

  2. Vincent
    Posted January 13, 2009 at 4:12 am | Permalink

    Thanks for this trick… But it doesn’t work when a widget is deleted from an area : area become empty, but ul element is displaying.

  3. Posted May 23, 2009 at 11:16 pm | Permalink

    Hi,

    Thanks for this tip, but as Vincent says in previous comment, it doesn’t work when a widget is deleted from an area : area become empty, but ul element is displaying.

    Any idea ?

    Thanks

  4. Posted June 3, 2009 at 12:55 pm | Permalink

    Thanks for this hint. This helps me!!

  5. Posted October 10, 2009 at 4:33 pm | Permalink

    Has this been made part of the 9.0 code? Thanks!

4 Trackbacks

  1. [...] Hiding the Sidebar Widget HTML when There’s no Widgets→ With it you can check to see if there are widgets in a sidebar and if not, well, you can do something else. Like nothing for an example. Nothing, in this case, is really something. [...]

  2. [...] take a look at Building with WordPress: From sketch to prototype to company website in 5 hours and Collapsing WordPress Widget-Ready Areas & Sidebars and Show latest comment(s) for each post on main page. Wordpress [...]

  3. [...] Collapsing WordPress Widget-Ready Areas & Sidebars This post covers how to create more versitile widget areas. These types of widget areas will help keep your markup valid when they are being used and when they aren’t. [...]

  4. By CCLab: PHP Blog Start on December 31, 2009 at 11:31 pm

    [...] page after I somehow deleted it and one for thesis notes and thoughts.  Both must be developed. http://themeshaper.com/collapsing-wordpress-widget-ready-areas-sidebars/ Some things I want to change: Type and arrangement. How future proof is the organization and [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. In addition, you may find yourself fitter, happier and more productive. Comment away.

Subscribe without commenting