Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

How to Add Widgets to your WordPress Themes

Adding widgets to a WordPress theme is a fantastic way to make things really easy to customise for the people who use the theme. Below is a really quick explanation of how to add them to your own themes.

Creating widget areas is actually really simple - there are only two steps in the process. The first is initializing the widget area, and the second is inserting the widgets into your theme.

Initialisation

You can actually use one of two functions to create widget sidebars, register_sidebar and register_sidebars. I prefer register_sidebar as this allows you to create multiple sidebars with different settings for each one.

$args = array (
    'name' => 'Sidebar Name',
    'id' => 'sidebar-id',
    'description' => 'Sidebar Description (shown in the Widget admin)',
    'before_widget' => '<li id=\"%1$s\" class=\"widget %2$s\">',
    'after_widget' => '</li>',
    'before_title' => '<h2 class=\"widgettitle\">',
    'after_title' => '</h2>'
);

The description is a new property - added in WordPress 2.9. It's a nice little addition that allows you to describe where and how the widget bar will be used in the theme.

Execution

Execution is easy. To execute the widget created in the code above you just have to pass the widget id to the dynamic_sidebar function.

dynamic_sidebar ('sidebar-id');

Full details on the WordPress Widgets API is available on the WordPress Codex.

6 Responses to “How to Add Widgets to your WordPress Themes” Leave a reply ›

  • Are all widgets created like this, or is this something new to WordPress 2.9?

    • Profile

      This is how the sidebar widget areas have been created since widgets were added to WordPress. There's a new parameter (description) but everything else is the same. What did you think was done differently?

  • I hope that template will last forever.

    Last week a blogger friend told me that it was no longer supported by WordPress. I looked and it did not come up, so I panicked and tried to change to something else, which was not too bad except that I could not deal with some of its extra features called "footers" which were long archives and tag lists hanging onto the end of the post duplicating some of the sidebar content.

  • So, where does this codes should be added?

    is it on functions.php?

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy