|

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

  • |

    Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images). function attachment_search( $query ) {…

  • |

    Quick Tip: Get a Family Cell Phone Plan to Save Money

    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 was talking to a friend yesterday who told me his parents booted him and his brother off their family plan…

  • | |

    Simplecast Episodes and New Projects Page

    Over the weekend, I announced on Twitter that I officially released a plugin I’ve been working on for a while called Simplecast Episodes, which allows you to add and embed podcasts hosted at simplecast.fm into WordPress. I’m happy to say that it’s officially in the WordPress Plugin Directory.

  • | |

    How I Launched Creator Courses in One Week

    7 months ago I took my business full-time. Being a freelancer for most of my life, and even doing it full-time for a while after college, I thought I had a pretty good handle on how things would work. But as it turns out, the product space is a lot harder to work in than…

  • |

    Using Back Tap on iPhone

    I had the pleasure of participating in the GoWP Niche Agency Owners Happiness Hour recently and we talked all about tools (I didn’t know that going in, but as you can imagine I was pleased). Something I got to demo was how to turn on iPhone Back Tap (a feature in iOS 14 and up)….

  • |

    Security vs. Usability

    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 problem that all web developers people in the computer field face is security. When creating your application, website, server,…