How to Use Custom Sidebars on Posts and Pages

Today I'd like to show you how to easily add custom sidebars to use within your posts and pages. It could be useful to display different widgets according to your page or post's topic.

We'll see how to use WordPress meta boxes to store and retrieve a chosen sidebar for a specific post. Custom sidebars will be added in the theme options section.


Introduction

I used to deal with the great widget logic plugin to display various widgets on different pages, but the widgets page became very fuzzy and difficult to maintain. So why not use several sidebars and choose which one to display on a specific post? That's what I'll cover in this tutorial.


Step 1 Adding Sidebars in the Theme Options Page (Twenty Eleven)

In this tutorial, I'll use the great WordPress default theme Twenty Eleven to demonstrate how to use custom sidebars in your theme.

First, we are going to add a new setting in Appearance -> Theme options. You can refer to this great tutorial to see how to add settings.

Open the theme-options.php file, located in twentyeleven/inc and add this code at the end of the twentyeleven_theme_options_init() function:

Then add a default value (an empty array) to the default theme options values, at the end of the twentyeleven_get_default_theme_options() function:

Now we are going to create the callback function that handles the setting's display.

Let's add some jQuery to handle interactions such as adding and removing sidebars, which are basically some list elements containing a hidden input. We'll also generate those list elements from already saved sidebars.

Eventually, add this code in the twentyeleven_theme_options_validate() function to sanitize and validate form input:

At this point, you should be able to manage and save custom sidebars within your theme. For instance, let's create a new custom sidebar called "My custom sidebar". Your theme options page should look like this:

Managing custom sidebars in the theme options section

Step 2 Register Custom Sidebars

Now we're able to add custom sidebars, we need to register them so they can appear in the widgets admin page.

In the Twenty Eleven theme, this is done within the twentyeleven_widgets_init() function. So at the end of this function add this:

We retrieve theme options, check that there is at least one custom sidebar and register it. We use a quick function to generate a slug out of the sidebar name to be used as the sidebar ID.

Now, go to Appearance -> Widgets and you should see our new custom sidebar.

Add widgets to custom sidebars

Step 3 Adding A Meta Box

Now our custom sidebars are available, we'll add a meta box to display a list of all sidebars available inside the post editing form.

If you're not familiar with meta boxes, you can refer to these links:

Now let's dig into the code.

Add Meta Boxes

First, we'll simply add our meta boxes. We need to declare two meta boxes, one for posts and one for pages. We also need to register two hooks, one to add meta boxes and the other one to save them.

So open your functions.php file and add this:

Create the Callback Function

Now, let's create the custom_sidebar_callback function, which will print out the meta boxes' markup.

There're several key steps in this function:

  • Retrieve all registered sidebars (theme default sidebars included) with the global $wp_registered_sidebars variable.
  • Get post metas
  • Create nonce security
  • Add a select element with all sidebars plus a default one which is defined directly in the template file.

Save Meta Box

Now let's save our post meta. Again several steps here:

  • Check WordPress is not autosaving
  • Check nonce and authorizations
  • Save post_meta

Now you should now be able to see this box in the post edit page, in the right column. If not, check that the custom sidebar box is displayed in the top screen options panel.

Choosing a sidebar in the custom sidebar meta box

Step 4 Adjust Template Files

Now everything is correctly set up, what's left to do is updating our templating files so that they can display custom sidebars.

Let's create a new page based on the sidebar template (available in the page attributes box). This template relies on the sidebar-page.php file. Edit this file and add these lines at the top (below the template's commented header):

We retrieve current post meta data to get the chosen sidebar.

To switch between a custom sidebar and the default one, change the get_sidebar() call at the bottom of this same file to this:

The get_sidebar($slug) calls for sidebar-slug.php. So what we have to do, is to create a file named sidebar-custom.php and add this inside:

Now your page should display the sidebar you chose. For instance, I added two widgets in my custom sidebar:

Page with custom sidebar

I won't cover how to apply this to posts as it's exactly the same behaviour, you just have to update the content-single.php file to manage custom sidebars.


Conclusion

That's all folks! Now you can use unlimited sidebars for your pages and posts. This is just one solution among others, but it's a quick and easy way to implement custom sidebars, so don't hesitate to leave a comment, share your ideas, and give feedback!

Tags:

Comments

Related Articles