Adding widgets to your WP theme
Most up-to-date themes should support widgets; anything you download from the official WP theme repository will do so. But what if you're running an old theme, or writing one yourself? Actually, adding widgetability (is that a word? you know what I mean) is pretty straightforward. You'll need to have an up to date install of WordPress*, and to edit two theme files.
Firstly, functions.php. Add the following after the first <?php:
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
More adventurous souls who know a bit of PHP could edit this to produce different HTML, but for most people using most themes, this will be just what you want as it is.
Next, sidebar.php. Add this near the beginning, after <div id="sidebar"> or something very similar:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
and this, near the end, before the final </div>
<?php endif; ?>
Anything you put between those two bits of PHP will only be shown in your sidebar if there are no widgets installed. Anything you put before them, or after them, will be shown in the sidebar in addition to your widgets, so if you prefer to edit the actual sidebar PHP file rather than using text widgets, make sure you're writing in the right place!
* Widgets were incorporated into core WP in 2006, so if this isn't working for you, you really ought to upgrade!







