WordPress Widgets: An Object-Oriented Approach The Widgets API provides a solid litmus test and example of how to get started with object-oriented programming in WordPress.

Years ago, I created the WordPress Widget Boilerplate aiming to be the following:

An organized, maintainable boilerplate for building widgets using WordPress best practices.

Since then, not much has changed regarding the Widgets API (which we’ll look at later in this post), but what I consider to be “best practices” has changed. Further, the degree to which I think this API is a solid example of introductory object-oriented programming in WordPress is high.

WordPress Widgets: The Widgets Boilerplate

It’s not because it uses a lot of object-oriented principles, it’s not because it uses modern standards (at least as far as modern PHP is concerned), but because it does use a few things that help us to recognize a few, say, signals regarding object-oriented programming in WordPress.

And this is something that shouldn’t be understated: If you’re looking for examples of object-oriented programming in WordPress, look for APIs that employ it.

Further, if you’re looking for ways to gauge you’re own level of evaluating a piece of code (let alone a code base) for the use of classes and some of the more advanced features of OOP, then why not have some sort of a litmus test to see how you’re doing?

WordPress Widgets: The Widgets API

And the Widgets API does just that.

WordPress Widgets: An Introduction

So in a smaller series than my last, I’m aiming to look at the Widgets API and to do a few things:

  1. show you the basic skeleton of a widget and why it’s object-oriented,
  2. discuss what things you should be able to notice and why,
  3. update the Widget Boilerplate directly on this site first and then push it out to GitHub,
  4. build a widget using the API with the boilerplate as the foundation for our work.

And in this post, we’re going to start with the first point above.

But First…

Before going into this post, I recommend reading the following posts:

  1. Two Pillars of Object-Oriented Programming: Part 1 of 2
  2. Two Pillars of Object-Oriented Programming: Part 2 of 2
  3. Abstract Classes, Part 1 – Abstracting Behavior
  4. Abstract Classes, Part 2 – Abstract Classes and Interfaces

Once done (or if you feel you already have a grasp on the topics), then we’re ready to go.

⚠️ Hey, Wait!

Thanks for your interest in this article! Note that it's available to members only. If you'd like to review this (and have access to all previous and future articles), check out the membership benefits.

What’s Next?

In the next post in this series, we’re going to look at the object-oriented nature of the Widgets API and what things you should be able to detect immediately by reading the code.

This is because it’s important to recognize certain object-oriented principles in practice and this is a good way to gauge if you’re able to do that or not. If you are, great! Then it’ll continue to help develop that muscle. If not, no worries – it still helps you develop that muscle.

And it will serve you well as we continue to get move more and more into object-oriented WordPress development through practical means.

The necessary theory has been covered. So let’s get started on actually putting it into practice.