Browse By

Creating a WordPress Widget

If you are reading this post, you are familiar with a wordpress widget because you can see multiple widgets displayed in the right hand sidebar of this theme. The Archives, Recent Posts, Blogroll, and Links are all wordpress widgets. If you are asking yourself, “why create a wordpress widget?” here is a short answer:

This blog, like many others, is run using wordpress (wordpress was downloaded 3,816,965 times in 2007 alone). WordPress has become increasingly popular thanks to the incredible features, themes, and plugins offered, both by wordpress.org, as well as third party developers, such as:. The administrative end of wordpress is very easy to use and at the same time very powerful. The blog has all the necessary features necessary to run a successful blog, as well as the ability to add onto the large set of tools included with the download and installation of a wordpress blog. One of the best features of wordpress in my opinion, is the ability to create plugins and widgets that suit your needs. Once the plugin or widget has been successfully created, implementation is even easier. So now that we have covered a little background information, let’s get started!

Steps

1. Create a php file and put it in the “wp-content/plugins” directory in your wordpress installation path.
2. The first information in the widget file is required to be there. Each line needs to be there and is fairly self-explanatory. These lines tell the wordpress plugins manager what the plugin is, who made it, what version it is currently in, and the links to provide support and updates. It looks like this:

/*

Plugin Name: Test Widget

Plugin URI: http://smseserver.com/blog/

Description: This is my first custom created widget.

Version: 1.0

Author: Scott Spear

Author URI: http://smseserver.com/blog/

*/

3. Create the function to initialize the widget. The widget_test_init() function checks if you can register custom widgets and then loads the actual widget function, in this case named “widget_test()”. The last line actually registers the new widget by calling the register_sidebar_widget() function.

function widget_test_init() {

if (!function_exists(‘register_sidebar_widget’)) {

return;

}

function widget_test() {

echo ‘This is the test widget.’;

}

register_sidebar_widget(array(‘Test Widget’, TestWidget’), ‘widget_test’);

}

4. After the widget is registered to the sidebar, the very last line will call the add_action() function to load the widget and show it in widget control in the admin section of your blog. The function looks like this:

add_action(‘plugins_loaded’, ‘widget_test_init’);

The final php file would look like this:

<?

/*

Plugin Name: Test Widget

Plugin URI: http://smseserver.com/blog/

Description: This is my first custom created widget.

Version: 1.0

Author: Scott Spear

Author URI: http://smseserver.com/blog/

*/

function widget_test_init() {

if (!function_exists(‘register_sidebar_widget’)) {

return;

}

function widget_test() {

echo ‘This is the test widget.’;

}

register_sidebar_widget(array(‘Test Widget’, TestWidget’), ‘widget_test’);

}

add_action(‘plugins_loaded’, ‘widget_test_init’);

?>