Skip to content

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.

Categories

WordPress

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

7 thoughts on “How to Add Widgets to your WordPress Themes Leave a comment

    • 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?

  1. 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.

  2. Pingback: Build Better WordPress Themes and Templates

Leave a Reply

Your email address will not be published. Required fields are marked *