Quick Tip: Add a Widget to WordPress
A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a simple widget that allows the user to place my bit of code anywhere in the sidebar they’d like. Here I’ll show you how to add a simple widget to WordPress.
Create the Callback Function
First we need to create the function. We will create a simple hello world function.
function jlc_hello_world(){
print "<h3>Hello World!</h3>";
}I’ve appended “jlc_” to the beginning so it doesn’t override any other WordPress function. Now with the function written, let’s widgetize it!
Making it a Widget
To make it a widget, we need the WordPress function register_sidebar_widget($title, $callback_function). We will add this line above our hello world function, using “Hello World Widget” and the name of our function as the argument. So we have:
register_sidebar_widget('Hello World Widget', 'jlc_hello_world');
And all of our code, which will go in the theme’s function.php file (or in your plugin file):
register_sidebar_widget('Hello World Widget', 'jlc_hello_world');
function jlc_hello_world(){
print "<h3>Hello World!</h3>";
}That’s it! Your widget has no options, but maybe that’s something I could cover in a different post.
