2

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?

3

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.

  • If the argument passed to is_active_sidebar isn't an integer, it's assumed to be the sidebar name, not the ID. – Milo Oct 11 '13 at 16:44
  • With to is_active_sidebar in the Codex it states that it will look for the sidebar name, ID or number. So by registering my sidebars with the name as the page name (which I probably should change to something more solid), is_active_sidebar() should look for the sidebar with the same title as "Test" – Howdy_McGee Oct 11 '13 at 17:17
  • Howdy_McGee and @Milo : see the edit – s_ha_dum Oct 11 '13 at 17:51
  • @s_ha_dum - you're right, it doesn't work if I pass is_active_sidebar a title (via get_the_title) or even a string - at least for me it isnt working. I'm able to access the boolean by passing it the sidebar ID. – Howdy_McGee Oct 15 '13 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.