SarahWidgetise Your Theme

Written by Sarah from Stuff By Sarah

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

if ( function_exists('register_sidebar') ) {
    register_sidebar();
}

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.

<ul>
    <li><h2>Widget Title</h2>
        <ul>
            <li>Widget Item here</li>
            <li>Another Item</li>
        </ul>
    </li>
    <li><h2>Widget Title 2</h2>
        <ul>
            <li>Widget Item here</li>
            <li>Another Item</li>
        </ul>
    </li>
</ul>

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:

if (function_exists('register_sidebar')) {
    register_sidebar(array(
        'before_widget' => '<li>',
        'after_widget' => '</li>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
}

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:

if (function_exists('register_sidebar')) {
    register_sidebar(array(
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
}

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:

if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()) :

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

if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()) :
    // enter HTML code here for items such as list categories, pages, recent posts etc.
endif;

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.

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on May 7th, 2009 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

8 Responses to “Widgetise Your Theme”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Rarst says:

    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.

  2. nicky says:

    Thanks for this useful code which makes the wordpress theme even more flexible, I need to workout on this and implement.

  3. Sarah says:

    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.

  4. Chris says:

    I’ll be using this, and part 2, very soon.

    Thank you Sarah. :)

Trackbacks

  1. [...] Read the full post at Widgetise your Theme. [...]

  2. [...] week I wrote about how to widgetise your theme, and explained how to turn your sidebar into a dynamic sidebar, allowing you to add widgets from [...]

  3. [...] who is less clued up on code, we can register it as a widget and then it can just be added to our dynamic sidebar in which ever location you want. So to register our function as a widget we use the [...]

  4. [...] who is less clued up on code, we can register it as a widget and then it can just be added to our dynamic sidebar in which ever location you want. So to register our function as a widget we use the [...]

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Webmaster Corner

 

Our Free E-Books

Site Partners