Home » BLOG » WordPress » How to create a custom WordPress sidebar

How to create a custom WordPress sidebar

posted in: WordPress

The sidebar is one of a popular feature on the website. Often, it uses for blog or news page. Some WordPress themes provide the ability to add the new custom sidebar for the theme. But some don’t.

If your current theme doesn’t provide the ability to add a new sidebar on your site, you have two options to do.

Option 1: Using the plugin. You can use the Custom Sidebars plugin which is free.

Option 2: Add the new custom sidebar without any plugins which I will show you here. You can add the code into your child theme or create a new sidebar plugin.

Add the new sidebar into your child theme

Register the sidebar

Adding a new sidebar on your theme, you can add the PHP code into functions.php on your child theme.

In your child theme folder may look like this:

  • functions.php
  • style.css
  • screenshort.jpg – optional

At functions.php, add the code below in order to register a new sidebar.

/**
 * Added by Apple Rinquest
 * Added on 4/2/2020
 * desc: add custom sidebar widget
 */
add_action('widgets_init', 'ar_news_sidebars');
function ar_news_sidebars()
{
	register_sidebar(array(
		'name' => __('News Sidebar'),
		'id' => 'ar-news-sidebar',
		'description' => __('Show news on sidebar'),
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));

        /** 
        // add another new sidebar
        register_sidebar(array(
		'name' => __('Another New Sidebar'),
		'id' => 'ar-another-sidebar',
		'description' => __('Another New Sidebar'),
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
        */
}

The code above, we register the new sidebar with the unique ID as “ar-news-sidebar“. I add the extra HTML code in the before_widget, after_widget, before_title and after_title properties for matching the current theme. You don’t need to add the extra HTML code if you don’t need it.

We register only one new sidebar. You can register more sidebars as you like. Simply, register another new sidebar. Please see the code above in the comment.

We use the “widgets_init” action which fires after all default WordPress widgets have been registered. The callback function name must be unique. In my case, I name it “ar_news_sidebars“.

Display the sidebar on the theme

In order to display our new sidebar on the frontend, we just add the simple PHP code in the template. In my case, I want to add the new sidebar into my custom news archive template(archive-news.php). I will add the PHP code below in the template.

<?php
if (is_active_sidebar('ar-news-sidebar')) {
     dynamic_sidebar('ar-news-sidebar');
}
?>

Add the new sidebar into your theme via Plugin

If you don’t use the child theme and don’t want to use it. You can add the new sidebar via the plugin.

  • First, create a new folder under the Plugins folder. You can name the folder whatever you want. I name it “ar-sidebars”.
  • Then create a new PHP file names “ar-sidebar.php” which is the same name as the folder name and this file will be the main file for our plugin.
  • Next, copy the PHP code below into the “ar-sidebar.php”
<?php
   /*
    Plugin Name: Custom Sidebar Plugin
    description: create new sidebar
    Version: 1.0
    Author: <a href="https://applerinquest.com/">Apple Rinquest</a>
   */  

// add new sidebar
add_action('widgets_init', 'ar_news_sidebars');
function ar_news_sidebars()
{
	register_sidebar(array(
		'name' => __('News Sidebar'),
		'id' => 'ar-news-sidebar',
		'description' => __('Show news on sidebar'),
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));

        /** 
        // add another new sidebar
        register_sidebar(array(
		'name' => __('Another New Sidebar'),
		'id' => 'ar-another-sidebar',
		'description' => __('Another New Sidebar'),
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
        */
}
  • Save the file then activate the plugin
  • On the frontend, you don’t see the sidebar showing yet. Because we need to add the PHP code for showing the sidebar on the template. Add the PHP code below into the template you want to show the sidebar.
<?php
if (is_active_sidebar('ar-news-sidebar')) {
     dynamic_sidebar('ar-news-sidebar');
}
?>

That’s it. If you want to improve your code when you create the plugin, you should check my post here.

Child theme vs Plugin

I prefer the Plugin option which I can disable the plugin anytime without touching the code again.