Makebeta

Web Developer Tools and Tutorials

Making A Wordpress 2.5 Sidebar Widget Plugin

April 22nd, 2008 · 12 Comments

Wordpress 2.5 And Widgets

A Wordpress Widget is a dynamic element that can be placed in the sidebars (or anywhere that is declared a sidebar) in your Wordpress theme. For example on this blog there is a right sidebar with these widgets: Search It, Recent Entries, Categories, Related Sites, Recent Visitors (by Feedjit), Recent Readers (by MyBlogLog). A widget can be any small modular piece of html (usually a list but not always) that can be stuck into a sidebar.

Widgets Make Your Plugin More Flexible

If you write your plugin as a widget your users will easily be able to move your plugin to which ever sidebar they want and arrange it with their other widgets how they want to. And they can do all of this without touching their theme’s code. This is great for users who don’t know how to modify a Wordpress theme template.

Sidebars Are Not Just Sidebars

Though they are called sidebars, they can actually be any block element of the page. A sidebar could be a heading, a footer, a section within a particular page, anything you want. It’s easy to declare a sidebar. Usually sidebars are declared within a theme’s functions.php file.

Declaring Your Sidebars – From Within A Theme

Update: There’s a great tutorial just on making your theme widget ready over here.
For example in a theme I’m working on I have 3 sidebars, normal left and right sidebars, and a top navigation bar that I’m also using as a dynamic sidebar for widgets. In my functions.php file I have:

register_sidebar(array(
    	'name'=>'top_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
    	'name'=>'left_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
    	'name'=>'right_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));

You can see some basic documentation on register_sidebar here. The name=>top_sidebar’ tells Wordpress what I’m calling my sidebar. This name will be displayed in the Admin interface under Design->Widgets->Current Widgets in the drop down list. You also use this name to place all the sidebar widgets within your them template. Users can choose to add widgets to any of the declared sidebars. The before_widget, after_widget, before_title, and after_title specify some html to wrap the widget in. This is handy if you need to style the widgets in a special way for your theme.

Using Dynamic Sidebars Within Your Theme

To use your sidebars within a theme you just call the dynamic_sidebar function. Here’s how I do it in my theme:

<?php if (function_exists('dynamic_sidebar')) {
dynamic_sidebar('right_sidebar');
} ?>

This tells Wordpress to call the display functions for all the widgets the user has added to the ‘right_sidebar’ sidebar.

A Hook To Hang Your Widget From

You’ll need to tell Wordpress about your plugin’s widget. To do this you declare a “plugins_loaded” action hook like so:

    function mycoolplugin_loaded()
    {
        $widget_ops = array('classname' => 'mycoolplugin_widget', 'description' => "A very cool widget for your sidebar." );
        wp_register_sidebar_widget('mycoolplugin_widget', 'CoolPlugin', 'mycoolplugin_widget', $widget_ops);
    {
    add_action('plugins_loaded','mycoolplugin_loaded');

In your widget you’ll want to replace mycoolplugin with the unique name you’ve chosen for your plugin. And you’ll want to replace “A very cool widget for your sidebar.” with the real description for your widget.

A Note For Uber-Geeks

So if you read the documentation here they will tell you to use register_sidebar_widget instead of wp_register_sidebar_widget. I found register_sidebar_widget really frustrating because I couldn’t get it to display the description of my widget (above “A very cool widget for your sidebar.”). The internal widgets like Pages and Categories use the wp_register_sidebar_widget to declare themselves. If your interested you can find the these functions in wp-includes/widgets.php.

Your Widget Display Code

In the code above we specified mycoolplugin_widget as the callback function for Wordpress to use when it wants to display your widget. So we’ll need to declare this function and use it to generate our widget’s HTML code.


function mycoolplugin_widget($args)
{
	extract($args); // extracts before_widget,before_title,after_title,after_widget
	echo $before_widget . $before_title . 'CoolPlugin' . $after_title . "<ul>";
	for ($i=0; $i<10; $i++)
	{
		echo "<li>List Item $i</li>";
	}
	echo "</ul>" . $after_widget;
}

My widget simply displays a header of “CoolPlugin” and then a list of ten items. Of course you’ll want to make your widget do something much cooler, but I’ll leave that to you!

Further Reading & Resources

Most of these resources a little out of date for Wordpress 2.5. Most of the screen shots show the admin widget pannel for 2.3 instead of 2.5.

Makebeta is community resource produced by MerchantOS. MerchantOS is POS Software that makes running your retail business easy by organizing your sales and inventory. There's no software to install and data backups are automatic. Get your point of sale and inventory control the easy way with MerchantOS.

12 responses so far ↓

  • 1 Le Chat Qui (The cat who...) // May 21, 2008 at 1:42 pm

    Hi !
    Thanks for this tutorial which I found really helpful.
    I still have a question though.
    I’ve been creating a 4th “sidebar” which I would actually like to be at the bottom of my blog but just before the footer. And I can’t find any way to do that : it seems to get stuck at the top of the page (right down after the header).
    Do you know what I can do to move it definitly down ?
    Thanks for your help
    ;-)

    =^.^=

  • 2 justin // May 21, 2008 at 2:06 pm

    sorry I can’t really help with that sort of specific problem. It’s probably because you’re not calling the sidebar in the write place in your templates.

  • 3 Le Chat Qui (The cat who...) // May 22, 2008 at 12:10 am

    Ok thanks anyway ! I’ll check that

    =^.^=

  • 4 d0k // Jun 19, 2008 at 9:00 am

    what about including javascript functions?
    (I’ve some problems on it also about my plugin but let’s focus on the widget)
    I’ve a two similar widgets that needs the same javascript functions.
    I could add that js to all the pages editing the header function, but that’s not the best way to obtain the results, may be some pages does not need that’s js.
    I could put it before the widgets but if some pages inclube both of the widgets, I’m going to have error due to function redeclaration.
    Looking to best result because it’s a plugin working on mu installation
    Any advice? Ideas?

    If you have also a tutorial about including common wp js function to plugin administration I’d be glad.

  • 5 Justin // Jun 19, 2008 at 10:16 am

    Unless you want to put the functions on every page you’ll have to do some clever stuff. What I’d do is put some script code just below the HTML for each widget that checks to see if the needed javascript is loaded and if not writes a tag to the header section dynamically. I can’t go into the details of how exactly to make that work because it’s too complicated.

  • 6 Oh, mudanças » nababu.org // Sep 21, 2008 at 8:04 pm

    [...] só que o Codex (documentação do WP) diz bem “en passant” sobre elas. Eu encontrei esse texto que é um tutorial sobre widgets e que me ajudou um pouco na atualização do meu plugin, [...]

  • 7 Blog: Makebeta | Bscopes Feeds // Feb 26, 2009 at 7:54 am

    [...] Blog: Makebeta tagged with: Post: http://www.merchantos.com/makebeta/wordpress/making-a-wordpress-25-sidebar-widget-plugin/ [...]

  • 8 Dr. Mike Wendell // Mar 25, 2009 at 3:04 pm

    Thanks for this. I too like the idea of adding in a description as well and see it as having to use wp_register_sidebar_widget instead of just register_sidebar_widget. My question though is if you use wp_register_sidebar_widget, do you have to use wp_register_widget_control or can you stay with register_widget_control?

    Thanks,
    -drmike

  • 9 Glenn // Mar 31, 2009 at 8:27 am

    To help people create Wordpress Widgets I created a service that turned most any PHP into a wordpress widget. There is not need to learn the ins and outs of Wordpress coding. I’ve also implemented the new way of coding wordpress Widgets that will appear in 2.8.

    Thanks
    Glenn

  • 10 Sidebar Widgets | NSLog(); // Jul 1, 2009 at 3:58 pm

    [...] how to make a sidebar widget? A quick Google search pointed me here, here, here, here, and of course here. Did I miss [...]

  • 11 How to make a Sidebar Widget « Knowing more about Wordpress // Sep 27, 2009 at 1:32 pm

    [...] http://www.merchantos.com/makebeta/wordpress/making-a-wordpress-25-sidebar-widget-plugin/ [...]

  • 12 Darren // Nov 18, 2009 at 4:33 pm

    good tutorial but your background makes my head hurt…just to let ya know

Leave a Comment