If you want to use widgets in your WordPress theme, or you’re a theme developer and want to make your theme ‘widget ready’, then all it takes is a couple of steps to add the necessary code in and your theme is good to go.
The Function
First we need to register the sidebar in our functions.php file (if you don’t have a functions.php file, create one). At the simplest form this is
[sourcecode language="php"]if ( function_exists(‘register_sidebar’) ) {
register_sidebar();
}[/sourcecode]
This will set your widgets up in an unordered list with the widget title in a h2 and the content in an unordered list eg.
[sourcecode language="html"]
-
Widget Title
- Widget Item here
- Another Item
-
Widget Title 2
- Widget Item here
- Another Item
[/sourcecode]
Of course, this markup isn’t to everyone’s liking. Personally I prefer to use h3′s instead of h2 headings. So to do this we can override the default settings using the following code:
[sourcecode language="php"]if (function_exists(‘register_sidebar’)) {
register_sidebar(array(
‘before_widget’ => ‘
‘after_widget’ => ‘
‘,
‘before_title’ => ‘
‘,
‘after_title’ => ‘
‘,
));
}[/sourcecode]
If you want to be able to target the widget’s output even more then we can add an ID and class to the widget using the following code:
[sourcecode language="php"]if (function_exists(‘register_sidebar’)) {
register_sidebar(array(
‘before_widget’ => ‘
‘after_widget’ => ‘
‘,
‘before_title’ => ‘
‘,
‘after_title’ => ‘
‘,
));
}[/sourcecode]
The ID is generated from the widget’s name and the class is generated from the widget’s callback.
So now we’ve got our sidebar set up in the functions file you can add widgets via the Appearance -> Widgets menu. Now we need to get them showing on the front end.
The Sidebar
The code required for the sidebar is a simple if statement that checks if we’ve registered a sidebar. If we have then it will show that, otherwise it will display what’s within the if statement. This is achieved using the following code:
[sourcecode language="php"]if (!function_exists(‘dynamic_sidebar’) || !dynamic_sidebar()) :[/sourcecode]
This simply says ‘if the function dynamic_sidebar() doesn’t exist, or there’s no output from the function dynamic_sidebar() display the following’. Within the if statement you place the code that you want to display if the user doesn’t add any widgets to their sidebar (even if you’ve got the functions file and sidebar set up, if no widgets are added then no output is made and so the if condition becomes true and the code within it will display.
So this gives us the final code of
[sourcecode language="php"]if (!function_exists(‘dynamic_sidebar’) || !dynamic_sidebar()) :
// enter HTML code here for items such as list categories, pages, recent posts etc.
endif;[/sourcecode]
Conclusion
This is a simple addition but with widgets becoming quite popular, it’s a good idea to add the code in to make your theme even more flexible.







You forgot to show one more parameter in array – name. When there are multiply widgetized areas (I have five at my blog) it is much easier to handle them by name than by Sidebar 1,2,3…
I also completely dislike that WP assumes that widgets go into list items. What if there is only one widget? Also some widgets ignore that and produce invalid markup. I set up no container for widgets at all, it helps that I am mostly using those with precise output setup.
Thanks for this useful code which makes the wordpress theme even more flexible, I need to workout on this and implement.
Hi Rarst, no the multiple sidebars is this week
WordPress assumes widgets go into a list as most should do semantically, but of course it gives you the option to override the settings so it's not so bad. At the end of the day, it's only creating default styling which will be fine for most users.
I'll be using this, and part 2, very soon.
Thank you Sarah.