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.

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

29 Mar 2009

The future of WordPress themes

A couple of weeks ago there was quite a lot of talk within the WordPress themes community about the future of WordPress. Ian Stewart started it, and then it spread around the blogosphere… so I thought I’d offer my rather...
13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
08 Aug 2013

The Death of WordPress Theme Frameworks

WordPress theme frameworks are on their way out. They’re dying a slow death. At least that’s what I think.Nathan Rice recently wrote an article with his thoughts about theme frameworks – in defense of them – however he clearly has...
22 Nov 2014

WordPress 4.1 Improvements for Theme Developers

WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...