"Disable Drupal blocks/regions" has no effect with some custom themes

I came across a problem with a panels page when I check the "Disable Drupal blocks/regions" box, the sidebars are supposed to disappear so that the content takes the full width. But it didn't work.

Switching back to Garland and the sidebars disappeared as expected. After a quick search, I found this post on drupal.org which is the same issue but one of the replies essentially says to hack core to get it to work, but that is not necessary.

In Drupal core, it only looks for sidebars named left and right. I have 4 (left_sidebar_primary, left_sidebar_secondary, right_sidebar_primary, right_sidebar_secondary), this allows me to style some blocks easier dependent on their weighting in the sidebars.

The workaround is very simple. All it requires is a few lines in your template.php file in your hook_preprocess_page() function.

If you haven't that function in your template.php file, then here is the necessary code:

<?phpfunction theme_name_preprocess_page(&$vars) {
  
// Code goes in here
}?>

The code which we need to enter is fairly simple.

<?phpfunction theme_name_preprocess_page(&$vars) {
  if (!
$vars['show_blocks']) {
    
$vars['left_primary'] = '';
    
$vars['left_secondary'] = '';
    
$vars['right_primary'] = '';
    
$vars['right_secondary'] = '';
  }
}
?>

Here we check to see if the $vars['show_block'] variable is FALSE, if it is then we set the content of all the sidebar regions to nothing.

If you would like to disable more regions, just add them as they are defined in your theme_name.info file.

One other thing to mention is that you need to make sure in your page.tpl.php file that you correctly add the code to not show any HTML markup if the region is empty.

<?php if (!empty($left_primary)): ?>
  <div class="sidebar-primary">
    <?php print $left_primary?>
  </div>
<?php endif; ?>
<?php 
if (!empty($left_secondary)): ?>
  <div class="sidebar-secondary">
    <?php print $left_secondary?>
  </div>
<?php endif; ?>