Check for Widgets in Widget-Areas

WordPress-Christmas-06Welcome to our 6th door of our Advent Calendar. 🙂

If you use widgets in your theme and this also accommodates a variety of hierarchies, you will quickly realize that it really only makes sense to output HTML for the widgets when the widget area is also used by the user.

Ian Stewart, the developer of the Theme-Framework Thematic found an elegant solution, but you can use it even without the framework.
You just need one function, which is similar to the Conditional Tags of WordPress. Put this function in your functions.php of your theme.

function is_sidebar_active($index) {
	global $wp_registered_sidebars;

	$widgetcolums = wp_get_sidebars_widgets();

	if ($widgetcolums[$index])
		return true;

	return false;

It uses the output of your widget area, if there is content.

A query could look like this in your template:

<?php if ( function_exists('is_sidebar_active') && is_sidebar_active('sidebar') ) { ?>
	<div id="sidebar">
			<?php dynamic_sidebar('sidebar'); ?>
<?php } ?>

The above example loads the content only, if the widget area with the ID sidebar has content, that means if widgets are used in this area.

Comments are closed.


  1. Chris


    thanks a lot for mentioning our code 🙂

    This snippet was build to prevent a fully styled but empty widgetized area.

    Last month I spoke to Kaspars Dambis the creator of the Widget Context plugin and asked him, if he could support this method with his plugin. The same day about 3 hours later I got a first working copy of his plugin.

    The latest version of his plugin supports the above mentioned code snippets.


  2. designfollow

    great, thank you.

  3. Justin Tadlock

    I’ve always loved this snippet of code and have used it for quite some time. But, an equivalent of this function was added in WordPress 2.8: is_active_sidebar(). So, there’s no need for a custom function any longer.

  4. Michael

    @Justin: Thanks for the tipp!

  5. Dameer

    Hi all!
    I was looking for a way to dynamically add sidebar widgets without involving WP’s default
    “if ( !function_exists( ‘dynamic_sidebar’ ) || !dynamic_sidebar( ‘Home Page’ ) )”
    However, it’s not easy to find out current sidebar index. WordPress does index them as “sidebar-1”, “sidebar-2”, “sidebar-3”, etc. in spite of the fact they might be named with “Home Page”, “Category Page”, “Footer Column Left”, etc.
    Otherwise spoken, you can’t rely on “is_active_sidebar()” without knowing sidebar $index (it’s a required argument!). So, prior to above code, I found the following to work fine as a current sidebar index detector:

    $how_many_widgets_here = 0;
    $which_widgets_here = array();
    $widget_area_name = 'Home Page'; // seek for number of widgets in this widget-ready area
    $required_sidebar_id = ''; // widget-ready area name/index assigned by WordPress
    $keyz = array_keys( $wp_registered_sidebars ); // get keys of all
    foreach( $keyz as $k => $v ) { // iterate all
    $single_arr = $wp_registered_sidebars[ $v ];
    foreach( $single_arr as $kk => $vv ) { // next level
    if( $vv == $widget_area_name ) {
    $required_sidebar_id = $single_arr[ 'id' ]; // get the match
    if( $required_sidebar_id != '' ) { // if not blank
    $all_widgets = wp_get_sidebars_widgets();
    $which_widgets_here = $all_widgets[ $required_sidebar_id ];
    $how_many_widgets_here = count( $all_widgets[ $required_sidebar_id ] );

    Yeap, the above code contains much more than required in this case, my intention is to leave it there coz someone could find it useful – who knows. What you will probably need here is actually “$required_sidebar_id” value.
    Now it’s safe to continue with:

    if( function_exists( 'is_active_sidebar' ) && is_active_sidebar( $required_sidebar_id ) ) {

    dynamic_sidebar( $required_sidebar_id );


    Hopefully it helps.