When writing the code for Advanced Comments Widget I had a few goals in mind:
- Make better comments list for my sidebar.
- Really dig into widgets.
- Make it so the widget was easily extensible.
Of course I looked in the repository first, but everything there was either overkill (way too much going on), or didn’t quite fit what I needed. So I decided to roll my own.
Building Better Comment Lists
Let’s face it, the default Recent Comments widget is kind of lame. The lack of an avatar and comment excerpt really don’t entice a viewer on your site to join the conversations happening on your content. What’s the point of showing recent comments if they’re not going to draw people in? The Advanced Comments Widget was built with the intent to provide an easy way to build comment lists for your site’s sidebars.
Digging into Widgets
I’ve built a few widgets in my years as a WordPress dev, but never really dug that deep into them. I wanted to use this opportunity to do just that; learn how to build a solid widget that would provide needed functionality and cool features for mine, and my clients’ sites.
I’ve been a WordPress developer for almost 10 years, and in that time I’ve become very biased to one thing; extensibility. I wish everything in WordPress could be extended/filtered/hooked, so when I build plugins, I try to put in as many filter/action hooks as possible. As you’ll see in the following documentation, the Advanced Comments Widget lives up to this philosophy.
So let’s dig in!
Normally, when I release a plugin, I break down its various classes/methods into micro-tutorials to explain how it was built, and how it could be replicated in another plugin. If you’d rather skip that and go straight to extending the Advanced Comments Widget, click here: Advanced Comments Widget: Extending the Widget. If you want to learn stuff, read on!
This plugin can be broken down into three classes, each with their distinct responsibilities:
This class is the main administrator of the plugin. It handles registering and loading the widget and the scripts and styles used in the site’s dashboard. I won’t delve too deep into its methods since it doesn’t handle any actual widget functionality. If you’d like to see it in all its glory, you can check it out on GitHub: class ACW_Recent_Comments