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') ) register_sidebar(array( 'before_widget' => '
', 'after_title' => '', ));
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:
'Home Sidebar', 'before_widget' => '', 'after_widget' => '', 'before_title' => '
', 'after_title' => '', )); ?> 'Post Sidebar', 'before_widget' => '', 'after_widget' => '', 'before_title' => '
', 'after_title' => '', )); ?> 'Page Sidebar', 'before_widget' => '', 'after_widget' => '', 'before_title' => '
', 'after_title' => '', )); ?>
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:
$sb, 'before_widget' => '', 'after_widget' => '', 'before_title' => '
', 'after_title' => '', )); } ?>
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 😉
26 responses to “WordPress and register_sidebar”
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.
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!
Laura and Jeremy you are welcome!
Thanks a lot! Now my theme is broken.
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.
[…] WordPress and register_sidebar – registersidebar, How long do you … […]
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.
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?
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.
Thanks so much for the very quick reply 🙂
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.
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.
i think that “non-coders” understand nothing in function.php 😉
I appreciate your generousity of sharing this alternative.
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).
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?
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?
Carolina, not easy without checking your code. Maybe a wrong character?
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!
[…] great, see my other widgets posts). Only trouble was that when I tried to use code from another developer to make widget placement easier, it broke my website, making posts and pages invisible, both on the […]
Sorry for the trouble. It was a snippet from a premium theme 😉
There really wasn’t any trouble, but I think the person who wrote the premium theme might appreciate the feedback.
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
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.
@Xcellence-IT: Thanks. It’s fixed now.
Two years later and your WP code is still giving. Thanks for sharing!