I want to check if there are sidebars rendered on the front end and if so output some body classes so I can style the page differently. I tried using is_active_sidebar but that just returns true if the sidebar contains some widget (is active) - I want to check if it exists on the page. How can I do this?

My code looks like this:

In my single.php

<?php get_sidebar(); ?>

In my sidebar.php

if ( ! is_active_sidebar() ) {
    // Add something here perhaps?

<div class="sidebar widget-area" role="complementary">
    <?php dynamic_sidebar(); ?>
</div><!-- #secondary -->

2 Answers 2


If your certain sidebar widget area is page-specific then I think you need to load classes only to those pages. If so, use is_page() with it:

if( is_page('that-page') && is_active_sidebar('your-sidebar') ) {
     $class = 'your-class ';

If it's true for all the inner pages other than the front-page, use:

if( !is_front_page() && is_active_sidebar('your-sidebar') ) {
     $class = 'your-class ';

I think this is the PHP way.

  • Thanks, maybe I'm not thinking correctly about this but I would like some more input. I'm thinking my approach, which would also be PHP, would be more flexible and scalable than checking each page like you suggest. I would like to somehow reference some function (in functions.php most likely) when is_active_sidebar returns false, like in my example, and then add the body classes. But I think when this renders, all functions have already been fired no?
    – Johan Dahl
    Commented Apr 13, 2015 at 8:40
  • If there is a widget assigned is_active_sidebar will always return true. So using this to add body_class() won't make your thing - even with filter like this. You have to go with page-specificity by any means. Commented Apr 13, 2015 at 9:04

I do think that the correct way of doing this would be purely with the WordPress api (in php) which would place all body classes before content is ever sent to the browser.

However, here is a different approach which uses a snippet of javascript and checks that get_sidebar is actually called.

add_action( 'get_sidebar', 'my_get_sidebar' );
function my_get_sidebar() {
    if ( is_active_sidebar( 'your-sidebar' ) ) {
        ?><script>document.body.className += ' with-sidebar';</script><?php

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.