WordPress: Create a Widget

030th Aug 2010Development, PHP, Wordpress, , ,

Creating widgets isn’t that difficult, and you probably could figure it out quite easily if you don’t already know how, but I’ll show you anyhow.

Our first step is to add an action that will call the method which actually tells our WordPress initialization that this plugin has widgets.

1
add_action ( 'plugins_loaded', 'plugin_init_widgets' );

Here, we’re simply telling WordPress that, when all plugins have been loaded, it needs to fire off the method “plugin_init_widgets” which we will provide.

1
2
3
function plugin_init_widgets() {
register_sidebar_widget( 'My Cool Widget', 'plugin_widget_cool' );
}

In that step, we’re telling WordPress that anytime it needs to display the widget we initialized in the first step, it’s going to call the method “plugin_widget_cool”. Not too difficult so far, eh? Well, here’s where the real magic happens.

1
2
3
function plugin_widget_cool() {
echo ('This is my new awesome widget!');
}

Inside of this method, you can include calls to the database, which we covered previously, or anything else you’d like.

So there you have it. A basic widget for your awesome new WordPress plugin.

No Comments Comments Feed

Add a Comment

Before you post, please prove you are sentient.

What is 8 times 4?