|

Quick Tip: Add a Widget to WordPress

Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don't hold it against me. One of the perils of having a 20+ year old website!

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.

Similar Posts

  • | |

    WordPress Multisite Domain Mapping on Media Temple (dv)

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!I recently stood up a WordPress Network/Multisite that I wanted to use for several different client “Quick Sites” – simple WordPress…

  • New Facebook Fun

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! For all the people out there with Facebook, an online social site for college and now high school students all…

  • |

    WordPress Helper Functions for Detecting IE

    The other day I was working on a problem where I wanted to check if a website was using a specific browser (in this case IE) and version (in this case 9 or below). I came up with 2 functions that would serve an a nice, reusable check for both. These can also be extended…

  • Types of classes, part 2

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Today I will continue my series on classes in Java. To review where we are, you can read this post. First…

  • | |

    My Everyday Things, Part 1: Workflow

    Recently I started following Everyday Carry, a blog dedicated to showcasing the items that people must have on an everyday basis. I decided that in an effort to blog more, I would do a short, 3 part series on the stuff I use everyday. The series will be broken up into 3 parts: today’s installment…