|

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

  • | |

    2015: Resolving to Do More, and Less

    Every year I publish my list of resolutions as well as a scorecard from the previous year. For 2015, I’ve redesign the site (well, I used HTML5 Boilerplate) and broke my resolutions down into 2 categories: “Do More” and “Do Less.”

  • |

    Book Review: HTML5 for Web Designers

    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! HTML5 for Web Designers by Jeremy Keith is a book I pre-ordered when it was first announced and I finally got around…

  • |

    How to Learn a New Programming Language

    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! As I said in the last two posts, Google I/O was truly inspiring. It got me to thinking about how…

  • |

    About Coding with AI…

    Telling an AI to make an app is not app development, just like telling a 5 year old to draw a Picasso isn’t a Picasso.

  • | |

    How I Built It is Officially Hosted by Castos!

    Castos is a fantastic WordPress-centric podcast hosting platform that I’ve been recommending in my courses and coaching calls. But I have a confession to make: since Castos launched after I launched my podcast, I’ve been using Libsyn. For a long time, I’ve considered making the switch, but the migration process always seemed so daunting. On…

  • | | |

    Learning HTML and CSS (with My New Book, Out Now!)

    I’ve always been a learn-by-doing kind of guy. Maybe it’s because I’m thinking about other things when something is explained to me, or maybe people are bad at explaining things. But either way, a concept doesn’t really sink in until I do it, or until I see an immediate application for it. When I learned…