Support » Fixing WordPress » is_dynamic_sidebar to return FALSE with multiple sidebars

  • Resolved Minkowski


    I’m building a site using wordpress that uses multiple sidebars – Header, Left, Footer, etc.

    What I want to do is to test whether there are any active widgets within any particular sidebar so that I don’t load the enclosing < ul > tags if there are none, and more besides, I also want a dynamic layout based on these tests.

    The problem is that I would have thought that is_dynamic_sidebar(‘header’) would return TRUE if there was only an active widget within the dynamic_sidebar(‘header’), but it seems as though it doesn’t accept a value and returns TRUE wherever a widget is placed within my sidebars.

    Any help would be appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • OK. There is probably a built in way but this is suiting me until I find it.

    I’ve built the function –

    function widgets_exist($sidebar){
    	$sidebars_widgets = get_option('sidebars_widgets');
    	if (count($sidebars_widgets[$sidebar])){
    		return true;
    		return false;

    and placed that into the functions.php and calling it in the templates with –

    if (widgets_exist('header')) echo "TRUE";

    As I said, it will do for now but I would have thought that there would be a built in way or a more elegant solution. But in the meantime if this helps anyone else that’s great.

    Wow, how stupid do I feel?

    if(dynamic_sidebar('header')) echo "you're quite stupid";

    Pretty stupid.

    Not stupid at all. The thing is, the dynamic_sidebar function does return FALSE if the sidebar isn’t dynamic, but if it IS, it seems to echo the whole dynamic sidebar. I can’t seem to make that work in an either/or scenario, so I’m going with your solution.

    Justin Tadlock


    WordPress God

    Yeah, dynamic_sidebar() does will not only return TRUE, but it will echo the entire widget area.

    I’ve been using this for some time now:

    if ( is_sidebar_active( 'header' ) ) // Do something
    function is_sidebar_active( $index = 1 ) {
    	global $wp_registered_sidebars;
    	if ( is_int( $index ) ) :
    		$index = "sidebar-$index";
    	else :
    		$index = sanitize_title( $index );
    		foreach ( (array) $wp_registered_sidebars as $key => $value ) :
    			if ( sanitize_title( $value['name'] ) == $index ) :
    				$index = $key;
    	$sidebars_widgets = wp_get_sidebars_widgets();
    	if ( empty( $wp_registered_sidebars[$index] ) || !array_key_exists( $index, $sidebars_widgets ) || !is_array( $sidebars_widgets[$index] ) || empty( $sidebars_widgets[$index] ) )
    		return false;
    		return true;

    I’ll have to try the solution above as well.

    I think is_active_sidebar() will tell you if the sidebar has any widgets (“is active”). Caution: though the codex suggests that it takes as arg a sidebar ID or name, using name does not seem to work. This seems to have been reported in Trac and a patch provided a while ago, but I am not sure where that stands.

    had same problem: is_active_sidebar() wasn’t working with sidebar name… but ravisarma is right, it works with ID. there was just one gotcha for me before getting it to work, pass the id without quotes, for example is_active_sidebar(3) works but is_active_sidebar('3') does not. thanks for the help all!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘is_dynamic_sidebar to return FALSE with multiple sidebars’ is closed to new replies.