How to Widgetize a theme that isn’t widgetized?

Posted on 19 October, 2007 by Peterandrej

Widgets are becoming more and more popular amoung Wordpress-users. Adding and removing elements in the sidebar have never been easier. Widgets are cool, and the number of available widgets increase day by day. Nowadays, almost every plug-in comes with widget-support.

But, what if you’re favourite theme doesn’t support Widgets? What then? You’ll likely choose another, but not anymore. Here is how you add widgets to your theme.

Widget-support are nothing more really then a few lines of code that you must add to your sidebar, or where ever you want the widgets. Plus, you must add a file called “functions.php” to your themes folder. But, first thing first: How does the widget code look like?

<?php if (function_exists(’dynamic_sidebar’) && dynamic_sidebar(1) ) : else : ?>

<?php endif; ?>

Thats all. First, you have a parameter which says that if the function dynamic sidebar exists, wordpress should execute it. And thats basically the first line. The seond line (<?php endif; ?>) tells wordpress that the function should stop here. That means that you could add things both before and after this line of code. And, even in between it if you want something displayed if the function doesn’t exist.

You could add this code to your theme exactly where you want it. Doesn’t have to be the sidebar, tough it perhaps makes best sense to have it there. But putting this code in the footer? No problem. Just open the file you want edited and paste the code into it.

Ok, now over to the file you have to add to your themes folder. We have previously looked at the code right? That said that if the function dynamic sidebar exists, wordpress should execute it. Well, in order for the function dynamic sidebar to exist, we of course have to add it.

The function is added through a file called “functions.php” and have the following lines of code in it:

<?php
if ( function_exists(’register_sidebar’) )

register_sidebar(array(

‘before_widget’ => ”,

‘after_widget’ => ”,

‘before_title’ => ‘<h3>’,

‘after_title’ => ‘</h3>’,

));
?>

Ok, thats the code. The first few lines just registrating the function, but the next four (’before_widget’, ‘after_widget’, ‘before_title’ and ‘after_title’) could be edited to fit the widgets to your theme. In this example, there is nothing before and after each widget, but I could have added <li> and </li> to make the widgets behave like a list. The title of each widget is styled with the <h3> parameter. Here you could easily changed these to <strong> and </strong> just to make titles display with a bold font. Or <i> and </i> to make them italic

To make this file, just copy the code above and paste it into a empty text-file, and save it as “functions.php”. There after you have to upload the file to your wordpress theme folder using FTP, typically you could find your theme folder at www.yourdomain.com/wp-content/themes/your-theme

And thats all actually. Easy, wasn’t it? Now you could easily add widget-support to any theme which doesn’t have it already. Remember that your Wordpress version also have to support widgets for this to work.

And the code? Well, copy and paste. Thats all.

Share this entry:

del.icio.us:How to Widgetize a theme that isn't widgetized? digg:How to Widgetize a theme that isn't widgetized? spurl:How to Widgetize a theme that isn't widgetized? newsvine:How to Widgetize a theme that isn't widgetized? blinklist:How to Widgetize a theme that isn't widgetized? furl:How to Widgetize a theme that isn't widgetized? reddit:How to Widgetize a theme that isn't widgetized? fark:How to Widgetize a theme that isn't widgetized? blogmarks:How to Widgetize a theme that isn't widgetized? Y!:How to Widgetize a theme that isn't widgetized?
digg-theme for wordpress