Skip to content

How to add extra sidebar to a WordPress theme?

To begin the tutorial, extract your theme into a folder and open it to see all the files. If your theme has only one sidebar, then most probably you will NOT find a file called functions.php in your theme folder. In that case you will have to create this file yourself. Just open notepad or any other code editor to start a new file. Put this code into that file:

<?php
if ( function_exists('register_sidebars') )
register_sidebars(2);
?>

Or add this code instead:

if (function_exists('register_sidebar')) {
register_sidebar(array(
'name'=>'Put the name of the sidebar HERE',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
}

Save the file as functions.php and put it in your theme folder. This piece of code actually tells WordPress to register two sidebars for you (See register_sidebars(2) in the code). If your theme has more than one sidebar, you will find the functions.phpfile already present in your theme folder. You just have to edit the number to your requirement and save the file. You can increase this number if you want more sidebars and if your theme’s layout can accommodate it. Now, when you go to your WordPress admin section and browse to the widgets under the menu item called presentation, you will see two sidebars listed there. You can drag your widget items into any of the sidebars.

Now comes the part where we actually build the sidebars. If your theme has only one sidebar, try to locate a file called sidebar.php in your theme folder. In this example, where we are trying to modify the theme for two sidebars, let’s rename sidebar.php to sidebar1.php and make a new blank file called sidebar2.php. Put this code into sidebar2.php and save the file:

<div>
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else : ?>
<?php endif; ?>
</div>

Or alternatively put this code:

<?php if (!function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Put the name of the sidebar HERE')) : ?>
<?php endif; ?>

So, we have the two sidebars ready but they have not been placed in the index.php file yet. Both these sidebars need to be called from the index.php file in order to include them in your theme. Just open the index.php (it could also be in header.php, footer.php, bottom.php or whereever you want to position the sidebar) file from your theme folder and locate the code that calls your sidebar file (sidebar.php earlier). It should look something like :-

<?php include (TEMPLATEPATH . '/sidebar.php'); ?>

Edit this code and change the words sidebar.php to sidebar1.php.

This takes care of the first sidebar. Now take a look at the index.php file carefully and find a suitable place to insert the second sidebar. This might involve modifying your layout or adding new divs. Once you find a suitable place, place the following code there :-

<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>

Save the index.php file and now preview your theme. You will see all the widgets that you placed in both your sidebars appearing on your website. If you have not placed any widgets yet, you will not see any change. There might be alignment errors but you will have to fix them yourself. You can add more sidebars in a similar way to your WordPress theme. I hope this tutorial helps some of you.


So, now especially for Twenty Fifteen Theme:

Add the following to functions.php

function twentyfifteen_widgets_init2() {
register_sidebar( array(
'name'          => __( 'Widget Area Right', 'twentyfifteen' ),
'id'            => 'sidebar-2',
'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen'     ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget'  => '</aside>',
'before_title'  => '<h2 class="widget-title">',
'after_title'   => '</h2>',
) );
}
add_action( 'widgets_init', 'twentyfifteen_widgets_init2' );'

After that copy sidebar.php from the theme, and add the following to it:

<div id="anothersidebar" class="sidebar">
    <?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
    <div id="widget-area" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-2' ); ?>
    </div><!-- .widget-area -->
<?php endif; ?>
Published inCodingWordpress

11 Comments

  1. rodrigo

    Before anyone try this, please beware of making a copy of the site.
    I tried to do it and for some reason, my website fell all down, i had to erase everything, i lost all my archives… i should have better choose another theme…

  2. julia

    Hi,
    I am using the “Companion” to edit CSS code, because when I tried to edit the original style sheet, it took down the website. However, I really need to add a 2nd sidebar to the theme, how would I do that using “Companion”?

  3. Robbiegod

    What if you name the sidebar like “sidebar-leftmenu.php” and then use get_sidebar(“leftmenu”) command from wordpress to add the sidebar.

    That’s the method i use to add sidebars to the page.

  4. mahmud

    hmm. you made this tutorial a bit clumsy to me (I am not so much good in english and coding). but the juice i have taken from it helped me to create a custom sidebar just below the category nav menu in my theme. i am using the widget to display latest news from my blog. thanx for great tutorial.

  5. ruu

    hi.. actually i got a little problem here.. my template haven’t any sidebar.php on it. so.. what should i do?

  6. ruu

    haha` i just didn’t notice a part of this tutorial :p thanks for sharing~!

  7. mellisa

    No issues for me, i used the second code. and it worked perfectly thanks a lot

  8. Urdu Shayari

    My theme has 1 which sidebar also have fuctions.php file ! what i should do ?

  9. Florida Wholesale Printing

    Just what I was looking for today. Thanks for posting this – all the other tutorials online have been taken down or people say they don’t work. Can’t wait to try this out.

Comments are closed.