Hiding sidebar when no widgets are present?

If there are no widgets on the right sidebar because either none are being used, or they’re included in the footer, I would like the sidebar to be hidden, so the main content is using the col-sm-12 class instead of col-sm-9.

Would using is_active_sidebar under roots_display_sidebar in config.php be the correct way to do this? I really don’t know if this is the correct method, and my PHP skills are really poor, so I’d appreciate some advice. Thanks.

The logic is a little backwards for that function to work out of the box here. You will likely want to write your own function that checks if the sidebar is active, then return the opposite. Something like:

function roots_sidebar_status($roots_sidebar) {
  return !is_active_sidebar($roots_sidebar[0]);
}

Then add the function to the roots_display_sidebar array as:

array('roots_sidebar_status', array('sidebar-primary'))

Didn’t really test this, but it should get ya started.

Thanks for the help. I created the function and then added it to the roots_display_sidebar array as you said, but I couldn’t get it to work. I can’t figure out what the issue is, am I just missing something obvious?

I’m trying to do the same thing. I used the code <?php if ( is_active_sidebar('sidebar-primary') ) { ?>
to hide the div if not widget is in that sidebar and it works but my content still does not carry over to the right area where no widget is. I just have a blank space where the widget would normally be.

I just got it working. I had to write my own function .

edit the /lib/custom.php script, and add this function:

function roots_sidebar_full_screen($sidebar_name) {
  if (is_active_sidebar($sidebar_name)) {
    // Classes on pages with the sidebar
    $class = ‘col-sm-8’;
  } else {
    // Classes on full width pages
    $class = ‘col-sm-12’;
  }
  return $class;
}

then edit the /base.php file in the root of your theme. Change line #22
FROM:
<main class="main <?php echo roots_main_class(); ?>" role=“main”>
TO:
      <main class="main <?php echo roots_sidebar_full_screen('sidebar-primary'); ?>" role=“main”>

Now save your files and upload them to your theme. Now when you remove the widget it will change the css class to col-12 instead of 8.

Next is to add a sidebar to the left side and do the same.