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.
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 is easy. To execute the widget created in the code above you just have to pass the widget id to the dynamic_sidebar function.
Full details on the WordPress Widgets API is available on the WordPress Codex.
Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.