WordPress Should Update get_sidebar to Work With Multiple Sidebars

I submitted an enhancement to wordpress yesterday, to allow get_sidebar to work with multiple sidebars. It’s an extremely simple fix, and you end up with a function that looks like this:

function get_sidebar($name=null) {
	do_action( 'get_sidebar' );
	if ( isset($name) && file_exists( TEMPLATEPATH . "/sidebar-{$name}.php") )
		load_template( TEMPLATEPATH . "/sidebar-{$name}.php");
	elseif ( file_exists( TEMPLATEPATH . '/sidebar.php') )
		load_template( TEMPLATEPATH . '/sidebar.php');
	else
		load_template( ABSPATH . 'wp-content/themes/default/sidebar.php');
}

By adding two lines and one optional parameter to the get_sidebar() function, you would be able to pass it a sidebar name. For example, get_sidebar(‘left’) would load the template TEMPLATEPATH . ‘sidebar-left.php’ allowing you to have multiple sidebar files as part of your template.

When I redesigned Xavisys a while ago, I organized the content as center, left, right. This allowed me to put both sidebars into one sidebar.php file. Then I ran into the Internet Explorer peekaboo bug, and realized I would need to reorder the content as left, center, right. When I looked into how to best accomplish that in wordpress, I realized how simple it would be to adjust the get_sidebar function to handle it, without altering it’s current functionality at all. I actually find it hard to believe that it’s not already in the code. Hopefully it will make it into the 2.5 release.

About Aaron D. Campbell

Owner and lead developer at BlueDog, Aaron has 10+ years of web development experience, it a regular core contributor to the WordPress project, and has released many WordPress plugins.
This entry was posted in Wordpress and tagged , . Bookmark the permalink.

10 thoughts on “WordPress Should Update get_sidebar to Work With Multiple Sidebars

  1. John says:

    Neat… thks for the hack.. I’ve been looking for this. Is there a way to enable the admin’s ‘widgets’ page switch between multiple sidebars ?

  2. Yes, if you register multiple sidebars, it will show multiple sidebars. I usually register them in functions.php (in the theme) like this:
    register_sidebar(array('name'=>'left'));
    register_sidebar(array('name'=>'right'));

    Then in each sidebar, you need to use the name when calling dynamic sidebar, like this:
    dynamic_sidebar('left');

  3. John says:

    Hi Aaron,

    Thanks for the help… really appreciate it so much ^^ .. that now I can show that off to my boss *:evil laugh*

  4. No problem. You took the time to find the answer, you deserve the credit!

  5. Hello all,
    and thanks for the hack.
    It seems that since 2.5, you have the ability to specify the sidebar you want to include.
    http://codex.wordpress.org/Include_Tags

    I prefer your hack though, since i find it more … elegant.

    Good work.
    Bye.

  6. John says:

    Hi

    Can you explain where i should place the above code and anything that i should change please, ive had this problem on 3 themes so far when i come to splitting up the files upon completion of the theme.

    Currently for a theme i have 2 sidebars, leftsidebar and rightsidebar.

    Ive tried

    and

    and for the rightsidebar too of course.

    Id be glad if you could explain how i use the above code please.

    Thanks

    John

  7. John: You shouldn’t need to do anything, my patch was included into WordPress 2.5 & 2.6, so it’s built in now.

  8. John says:

    Hi Aaron

    Thanks for replying, i currently have 2.5.1 installed locally and unfortuntely im still having the problem, il keep on looking, i must be doing something wrong somewhere.

    Thanks

    John

  9. zacheos says:

    Some people use a hierarchy method in the naming of the template files to achieve multiple WordPress sidebars, but in my opinion it is too cumbersome for most user’s needs. The method of multiple sidebars for WordPress that I use involves only a very simple PHP snippet added to the template to call the desired sidebar file depending on the area of the site that you are in.

  10. zacheos: This is actually in core now. There are a couple reasons for using get_sidebar() rather than just including the file. First, it fires the get_sidebar action, which some plugins use. However, you COULD manually fire that if you wanted. The other thing it does is allow some fallbacks, using an unnamed or even default sidebar, rather than failing. Of course, you could do that too. The thing is, if you do both, why not just use it? ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: If you are replying to another commenter, click the "Reply to {NAME} ↵" button under their comment!