
Beginning WordPress 2.2, widgetized sidebars have been a feature available in WordPress. This allows users to simply drag-and-drop new “widgets” onto their theme’s sidebar (through an interface in the Admin) with little work for the theme developer. A theme developer just has to add a relatively small amount of code to their theme if they want their sidebar to be widget-enabled.
The steps to make your theme widget-friendly are simply and quite. You’ll need to specify some parameters in your functions.php file and add some code to your sidebar.php file. Let’s start out with the code for your functions.php file. You’ll be setting the parameters before_widget, after_widget, before_title and after_title. These will contain HTML markup will will be wrapped around the widget appropriately. Let’s take a look at a basic example of this. This assumes you’re using a ul for a sidebar. Obviously wrap this in php tags.
[php]if (function_exists(‘register_sidebar’)) {
register_sidebar(array(
‘before_widget’ => ‘<li class="widget">’,
‘after_widget’ => ‘</li>’,
‘before_title’ => ‘<h3>’,
‘after_title’ => ‘</h3>’,
));
}[/php]
In your sidebar.php you’ll test to see if widget’s are enabled or if they exist or not (version of WP older than 2.2). If that test is false, then you’ll display a default, static sidebar instead. Let’s look at a basic example of this. Add php tags where necessary.
[php]// Beginning of sidebar markup
if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar() ) {
// Default, static sidebar if widgets aren’t enabled
}
// End of sidebar markup[/php]
There you have it, a widget-friendly theme. Next week we’ll be covering how to develop widgets.




