Hybrid Core comes packaged with several default sidebars that themes can immediately register support for. These sidebars fit within many standard areas of themes. Themes can pick and choose which of these sidebars it’d like to use. Or, a theme may opt to not use any of the default sidebars at all by not registering support.
Each of the default sidebars were carefully chosen as common sidebar areas that would be useful for a large number of themes. The goal is not to have every sidebar in the world. It’s to provide a standard list of sidebars that themes may use.
You are, of course, welcome to register additional sidebars within your theme using the WordPress
register_sidebar() function. The defaults are there for a starting point that will cover a large majority of use cases.
Note that this feature does not actually display the sidebars. It merely registers them and allows them to appear on the Widgets screen in the WordPress admin. As a theme developer, it’s your responsibility to handle how these sidebars are displayed. If you’re unfamiliar with this process, please read Sidebars in WordPress.
Registering support for sidebars
In your theme setup function, you would add this line of code to register support for the framework’s sidebars:
add_theme_support( 'hybrid-core-sidebars', array( 'primary', 'secondary' ) );
What we’ve done here is registered support for the “Primary” and “Secondary” sidebars. You can register support for several other sidebars. Each sidebar has a fairly specific purpose, so your theme should try to stick to the conventions laid out here.
- Typically shown on the side of the page as the main sidebar.
- Typically shown on the side of the page within three-column layouts or as an alternative to the primary sidebar.
- Typically shown somewhere above, within, or below the footer area.
- Easy enough. Put it within the header.
- Should be shown directly before the post content on a page.
- Should be shown directly after the post content on a page.
Let’s suppose you wanted to register support for the
after-singular sidebars. You’d use the following code to register support for them.
add_theme_support( 'hybrid-core-sidebars', array( 'primary', 'secondary', 'after-singular' ) );
That’s all there is to it. The first parameter of the function is the feature name,
hybrid-core-sidebars. The second parameter is an array of the sidebar IDs you want to support within your theme. Note that you must add in the array of sidebar IDs you want to support. Otherwise, no sidebars will be registered.
Using sidebars in your theme
As a theme developer, you should already be familiar without outputting sidebars within themes. If not, I’ve written a more in-depth tutorial on sidebars in WordPress that you should read. This section will just be a quick and basic overview.
I like to split my sidebars into separate files to keep everything neat and tidy within my theme. Suppose you wanted to create a file for your “Primary” sidebar. To do this, you’d create a template named
sidebar-primary.php and place the following code within the file.
<?php if ( is_active_sidebar( 'primary' ) ) : ?> <div id="sidebar-primary" class="sidebar"> <?php dynamic_sidebar( 'primary' ); ?> </div><!-- #primary --> <?php endif; ?>
What the preceding code does is use the
is_active_sidebar() function to check if any widgets have been assigned to the primary sidebar. If so, it outputs some HTML and displays the sidebar widgets using the
dynamic_sidebar() function. Of course, you’re free to change this up to whatever suits your needs.
For the sidebar to actually display anywhere on your site, you’ll need to call the
sidebar-primary.php template using the
get_sidebar() function. I typically add this at the top of my
footer.php template, but you can add it wherever it best fits your theme.
<?php get_sidebar( 'primary' ); ?>