Please note that the information in this post may no longer be accurate or up to date. I recommend checking more recent posts or official documentation for the most current information on this topic. This post has not been updated and is being kept for archival purposes.
The Roots Theme for WordPress is awesome for creating great websites. I’ve built many of the sites in my portfolio on Roots and actually have one showcased in the Roots Gallery (Vets2Go). One situation I come across every so often is the need to reverse the logic of the Roots sidebar.
Roots Sidebar Explanation
Roots has conditional checks that if return true won’t show the sidebar. So, for instance take a look at the standard roots_display_sidebar
function in config.php
:
function roots_display_sidebar() { $sidebar_config = new Roots_Sidebar( /** * Conditional tag checks (http://codex.wordpress.org/Conditional_Tags) * Any of these conditional tags that return true won't show the sidebar * * To use a function that accepts arguments, use the following format: * * array('function_name', array('arg1', 'arg2')) * * The second element must be an array even if there's only 1 argument. */ array( 'is_404', 'is_front_page', 'is_page', ), /** * Page template checks (via is_page_template()) * Any of these page templates that return true won't show the sidebar */ array( 'template-fullwidth.php' ) ); return apply_filters('roots_display_sidebar', $sidebar_config->display); }
All the arguments below instruct the Roots core to not display the sidebar.php file. We want to reverse this logic so that the arguments will display the sidebar.
This is because our certain template needs to be full-width according to the design and functionality. No sidebar, unless I tell it so in the arguments above. Make sense?
Reverse the Sidebar Logic
If we take a gander into the Roots core we will find the Roots_Sidebar
class in lib/sidebar.php
:
class Roots_Sidebar { private $conditionals; private $templates; public $display = true; function __construct($conditionals = array(), $templates = array()) { $this->conditionals = $conditionals; $this->templates = $templates; $conditionals = array_map(array($this, 'check_conditional_tag'), $this->conditionals); $templates = array_map(array($this, 'check_page_template'), $this->templates); if (in_array(true, $conditionals) || in_array(true, $templates)) { $this->display = false; } }
Change public $display = true;
to public $display = false;
on line 17 in the file.
Next, change $this->display = false;
to $this->display = true;
on line 27.
Jump Back Into Config.php
Now that our logic is reversed we need to update our arguments to display the sidebar only on the pages we wish:
function roots_display_sidebar() { $sidebar_config = new Roots_Sidebar( /** * Conditional tag checks (http://codex.wordpress.org/Conditional_Tags) * Any of these conditional tags that return true won't show the sidebar * * To use a function that accepts arguments, use the following format: * * array('function_name', array('arg1', 'arg2')) * * The second element must be an array even if there's only 1 argument. */ array( //'is_404', //'is_front_page', //'is_page', ), /** * Page template checks (via is_page_template()) * Any of these page templates that return true won't show the sidebar */ array( 'template-sidebar.php' ) ); return apply_filters('roots_display_sidebar', $sidebar_config->display); }
Now only pages that use template-sidebar.php will display our sidebar. This is exactly what I need for a full-width layout with some pages using a sidebar.
Anything Else?
Hopefully this solves your issue with the Roots sidebar full-width by default problem. Check out this thread over at the Roots Google group for more information into the source of this information. Thanks to Catherine Azzarello and iamboris for first uncovering the problem and solving it with this elegant solution.