is_active_sidebar() Always Returns False

I’ve never gotten is_active_sidebar() to work, no matter if there’s widgets in the sidebar or not I always get false returned. Currently I’m creating a sidebar for each top level page:

http://pastebin.com/fX3Rv20f

I create my own widget(s):

http://pastebin.com/Bz9hv41z

And I’m testing whether the sidebar is active like so:

if(is_active_sidebar(get_the_title()))
    echo 'active';
else
    echo 'not active';

Even when I manually put the title in: is_active_sidebar('Test'); it always returns false. Am I using the conditional wrong? Do I need to add some sort of setting? Why would is_active_sidebar() fail?

1 Answer
1

The is_active_sidebar('Test'); function works correctly if the correct slug is used.

I think that the problem is that you are constructing the sidebar ID like this:

$sidebarID = preg_replace("/[\s_]/", "-", strtolower($page->post_title));

Then prepending sidebar- to it …

register_sidebar(array(  
                      'name' => $page->post_title,  
                      'id'   => 'sidebar-'.$sidebarID, 
                      //

But you are using the unmodified title as the slug when you check …

if(is_active_sidebar(get_the_title()))

You need to alter your code that you are checking for the correct sidebar slug. you need to be consistent.

Edit: As stated, the “function works correctly if the correct slug is used”. While the Codex does state that the “Name” is a valid parameter value, using the name does not work, at least not when I try to use it. Proof of concept (mostly copied from the Codex):

$args = array(
    'name'          => 'My Sidebar',
    'id'            => 'my-sidebar-id',
    'description'   => '',
    'class'         => '',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>' 
); 
register_sidebar($args);

With a sidebar registered with the above code, and a widget in the sidebar, the following returns false.

var_dump(is_active_sidebar('My Sidebar'));

While using the ID returns the correct result– true:

var_dump(is_active_sidebar('my-sidebar-id'));

I have not done enough research to determine whether the Codex is wrong, or whether there is a bug in the Core. However, using un-normalized data like a post name is probably a bad idea in either case.

Leave a Comment