0

I have looked here on WA and also on the codex but there is no definitive source that I could find.

Which is the proper/best way to check to see if a dynamic_sidebar is active and contains a widget?

I see these 3 solutions at large but not sure what the proper way is:

 1. if(is_active_sidebar('foo')){ //check if the sidebar is active
        dynamic_sidebar('foo');
 }
 2. dynamic_sidebar('foo'); //basically just call it
 3. if(dynamic_sidebar('foo')) //also have seen the inverse if(!dynamic_sidebar('foo') check the return of the function

If they are all valid then why not just choose option 2?

1
  • is_active_sidebar can be used in case if you want to wrap the sidebar within some html codes, ex - if( is_active_sidebar('foo') ){ echo '<div id="sidebar-foo">'; dynamic_sidebar('foo'); echo '</div>'; }
    – Shazzad
    Commented Jan 6, 2014 at 23:12

1 Answer 1

1

dynamic_sidebar() produces sidebar output. So case by case:

  1. Checking if sidebar is active is pointless because otherwise it would just do nothing anyway. This check is useful if you need to perform it elsewhere for something different and/or more extensive than just calling it.

  2. Calling it just works. << so this is what you want

  3. I am not sure what this is supposed to be, but it is rather pointless. If it works in first half then second half is not called. If it doesn't work in first half second half won't do anything either.

3
  • for #3 i meant for that to mean if dynamic or if not dynamic; not that they are used at the same time. i will update the question to be clearer.
    – Shawn
    Commented Jan 6, 2014 at 23:15
  • @Shawn while it does return true or false it is pointless as conditional because it will actually spit out sidebar itself each time it's called. If you want to only check and not output sidebar use is_active_sidebar().
    – Rarst
    Commented Jan 6, 2014 at 23:18
  • i was thinking that myself, but thats why i posted the question at large here on WA. I just wanted to make sure that my code handles things gracefully. I was using #2 but wanted to make sure. I appreciate the answer.
    – Shawn
    Commented Jan 6, 2014 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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