|

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

  • Tools I’m Using to Avoid Spoilers

    Between a certain Game of Thrones and Avengers: Endgame, I’ve been seeing many complaints about things being spoiler on social media. Even with the lengths I went through when Infinity War came out, it was still spoiled because a careless YouTuber (me) let autoplay happen unregulated, and another YouTuber (the guy in the video) opened…

  • | |

    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…

  • Coding Projects as my Side Hustle

    I did some thinking over the weekend, after I wrote the post about my learning plan. I looked at my project pipeline. I looked at what people were hiring me to do, and I reviewed the next few personal projects I’m working on. You know what I saw? No coding projects. Honestly, I shouldn’t. That’s…

  • | |

    Creating a Responsive Gutenberg Price Table

    Last week I worked on an upcoming tutorial for a popular online publication on how to style the Gutenberg Columns block (I’ll be sure to send that along when it comes out). As as result, I decided to experiment to see what you could reasonable do, and came up with this Gutenberg Price Table: https://codepen.io/jcasabona/pen/RYvEYd. In…

  • |

    WordPress and .gitignore

    Updates have been added after the jump! In the coming weeks I’ll likely put a lot of my own personal code (as in, not code people pay me to write) in public repos on Github. A lot of my work is WordPress related so I’ll make a local repo at the root of some WordPress…

  • |

    Simplecast Episodes Version 0.6

    Over the weekend I released Version 0.6 of my @simplecast plugin, Simplecast Episodes. Aside from some smaller internal changes, I added what I think is a pretty nice feature to coincide with WordPress 3.9’s new feature to create media playlists from audio and video: the ability to download episodes directly into WordPress, adding them to the…