WordPress and register_sidebar

Everyone who is building WordPress Themes probably implement the widget function in their theme. Code for the widgets is in functions.php. Here a code snippet of the default theme:

if ( function_exists('register_sidebar') )
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',

As we all know, there is the function register_sidebar since version 2.2. My question is: How long do you want to ask if this function exists? The default theme of WordPress 2.7 doesn’t ask, if have_comments (a new template tag of version 2.7) exists, but it asks if register_sidebar exists. I just suppose the developers just forgot it and everybody in the world thinks, that it has to be like that.

Please don’t misunderstand, the query isn’t wrong, but in my opinion just useless in the default theme of version 2.7

Since we are already talking about the register_sidebar and it’s use, the code of a premium theme uses this query even 3 times:

if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'Home Sidebar',
'before_widget' => '<div class="block">',
'after_widget' => '</div><div class="blockfooter"></div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'Post Sidebar',
'before_widget' => '<div class="block">',
'after_widget' => '</div><div class="blockfooter"></div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'Page Sidebar',
'before_widget' => '<div class="block">',
'after_widget' => '</div><div class="blockfooter"></div>',
'before_title' => '<h3>',
'after_title' => '</h3>',

This is not really a good example for a premium theme. First, why closing the PHP tag after every function and open it before every function again?
Second, if the function register_sidebar exist in first place, it will also exist the 2nd time
and third, the code is almost identical except the name of the sidebar. That could be done much easier:

$sidebars = array('Home Sidebar', 'Post Sidebar', 'Page Sidebar');
foreach($sidebars as $sb) {
	register_sidebar(array('name'=> $sb,
		'before_widget' => '<div class="block">',
		'after_widget' => '</div><div class="blockfooter"></div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',

As I said, it’s not wrong and I didn’t mean it as it is a bad way to code, but sometimes you can do it much easier ๐Ÿ˜‰

Comments are closed.


  1. Laura

    That is so handy! I was looking for a way I could name the sidebars according to which templates they are associated with, as previously I could only find ways to call them Sidebar 1, Sidebar 2 etc.


  2. Jeremy

    Great tutorial, I new there had to be a cleaner way of doing this. The fewer the lines of code the better I say. Thanks!

  3. Michael

    Laura and Jeremy you are welcome!

  4. Anthony

    Thanks a lot! Now my theme is broken.

  5. Ryan

    Lol, good point. My own themes all include the IF statement. But as you point out, it is redundant now that WP 2.2 is so old.

  6. Michael

    Anthony, i think, you did something wrong. That was not a tutorial, only a way to make it better. If you want to use it in your theme, you have to change the names of the sidebars to your names and the html code to your html code.

  7. Sarah

    Thanks for this.
    It really makes one write less code.
    one question though,
    what happened to :

    if ( function_exists(‘register_sidebar’) )

    shouldn’t it still come before the code you provided?

  8. Michael

    Sarah, the function register_sidebar is in WordPress core since version 2.2. So i think you dont have to ask if the function exists.

  9. Sarah

    Thanks so much for the very quick reply ๐Ÿ™‚

  10. Monika

    Hi Yes the short code looks fine, but if I give a theme away I’m using the long version, because the long version is easier to understand for *non-coder* .

    Maybe he would like to have one widget without div class block, he doesn’t know how to do this if he see the short version.


  11. bagheadinc

    This is a pretty nifty little trick. I’m not sure why, but when I use the variable $name it makes all of my post queries come back empty. I just used $sidebar instead of $name and it works perfectly.

  12. Michael


    i think that “non-coders” understand nothing in function.php ๐Ÿ˜‰


    right done!

  13. Arian Xhezairi

    Excellent way.
    I appreciate your generousity of sharing this alternative.

  14. Leonard

    Awesome post, really answered a lot of questions but I would say integrate bagheadinc’s comment into the actual post as I tried the code and it killed off my posts on the front and back ends. Changed $name and it worked a charm.

    Also in regards to Sarah asking about the if function exists line, surely if your creating themes for the public then its handy to have these in for older versions people are using, maybe accompanied by an else echoing a message to get updated! I’ve only really just got to the meat of wordpress and I love it but knowing what some clients are like, some won’t pay for someone to update their wordpress (or even think of it) and are scared to do it themselves (until of course all the exploits get used and they get their site hacked).

  15. Leonard

    Apologies for bringing up that main question for the post about whether we should use function exists, was here looking for other information and scanned over it, only just going back and realising how arrogant it probably sounded.

    To comment on it again I guess its like with making sites for older browsers, how many versions should you support before you just have to leave behind those who have neglected to keep up? The default theme in wordpress may have dropped some code in recent versions but having been distributed purely with that version does it really mean template makers in the world at large should?

  16. Carolina

    Hi, I will try to make me understand in english (iยดm from Argentina), my problem is that when i try to change the code in function.php, its shows me a notice “Warning: Cannot modify header information – headers already sent by (output started at /home/deseostr/public_html/wp-content/themes/japanese-cherry-blossom-10/functions.php:9) in /home/deseostr/public_html/wp-admin/theme-editor.php on line 70”
    The thing is that i actually changed teh code yesterday, and the day before and nothing appeared…
    Someone knows what can i do?

  17. Michael

    Carolina, not easy without checking your code. Maybe a wrong character?

  18. Terry Kernan

    I can confirm that the use of the $name variable in the lean version above breaks my theme in wordpress 2.8, changing it to $sidebar done the trick!

  19. Michael

    Sorry for the trouble. It was a snippet from a premium theme ๐Ÿ˜‰

  20. Terry Kernan

    There really wasn’t any trouble, but I think the person who wrote the premium theme might appreciate the feedback.

  21. Roger Coathup

    Putting together a theme with around 20 named sidebars… the foreach loop (obvious, but so easily overlooked in all the other register sidebar code examples) is very helpful… thanks

  22. Xcellence-IT

    Its good and removes lot of code while similar sidebar are required. But you must upgrade your tutorial to change $name to $sidebar because original version break my theme.

    BTW, thanks for this great piece of code.

  23. Michael

    @Xcellence-IT: Thanks. It’s fixed now.

  24. David Radovanovic

    Two years later and your WP code is still giving. Thanks for sharing!

2 pingbacks

  1. EMPFEHLUNG | Nettes Wordpress Plugin - BildEsel | one3p netBLOG
  2. wordpress no posts / pages found | giblitz