Software Engineering in WordPress, PHP, and Backend Development

Tag: Object-Oriented Programming in WordPress (Page 4 of 6)

WordPress Widgets: How to Detect Object-Oriented Programming

If you’ve not read the first post in this series, I recommend it, as we’re starting to get into writing object-oriented code for WordPress through the use of the Widgets API.

The series is going to capture 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.

But before doing that, I want to make sure that everyone reading this is caught up on the core principles of object-oriented programming and has everything needed to build out an object-oriented solution for WordPress.

To that end, I recommend the following:

  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
  5. The Independent WordPress Developer

If you’ve read all of that content, great. You’re going to be well-prepared for this post and the upcoming posts. If not, there may be some holes in the rest of what you’re about to read, but the gist of the post should be clear enough.

What’s The Deal, Exactly?

Here’s the thing: Last week, I shared a bit of code along with some information about the Widgets API. I’m going to be revisiting that a little bit more in this post before we get into the more coding intensive part for two reasons:

  1. I want everyone reading this to be on the same page as it relates to writing object-oriented code (at the very least, in this context),
  2. I recognize that people are coming from different backgrounds and I want to make sure we’re all on the same page as much as possible before proceeding.

If you have experience with writing object-oriented code, especially in an advanced capacity, this may seem simpler to you; otherwise, I hope this is going to arm you with everything you need to detect object-oriented practices not only concerning this API but when reading others’ code, too.

Continue reading

WordPress Widgets: An Object-Oriented Approach

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.

Continue reading

Writing Unit Tests with PHPUnit, Part 3: XML Configuration

In the past posts in this series, I’ve covered the following two topics:

  1. Writing Unit Tests with PHPUnit, Part 1: The Set-Up. A guide to getting started with writing PHPUnit tests through the use of a basic cache and using the setUp method of the framework.
  2. Writing Unit Tests with PHPUnit, Part 2: The Tear Down. A tutorial on how to write unit tests that properly leverage the setUp and tearDown methods of PHPUnit.

Each of the above is meant to provide a primer for how to get started with writing very basic unit tests. Things can get more complex especially as an application or project grows (but that’s always true, right?).

But to make sure one is prepared for that, there’s one final component to unit testing that I believe we should focus on and that’s understanding the PHPUnit XML Configuration File (which you may have seen in other projects as phpunit.xml).

Continue reading

Writing Unit Tests with PHPUnit, Part 2: The Tear Down

Late last month, I started talking about writing unit tests in PHPUnit for WordPress-based code. This primarily included the idea of setting up PHPUnit, the setUp function, and writing basic tests.

This did not, however, discuss what I know about the tearDown function which is still an important feature of writing using tests. Further, there’s also two ways to consider writing tests for WordPress projects.

Namely:

  1. writing tests specifically for plugins and application-layer functionality,
  2. running unit tests against the WordPress application.

Before moving forward with this particular post, though, I recommend catching up on what I’ve covered thus far. This includes the following posts:

  1. A WordPress Development Environment (Using a Package Manager)
  2. An IDE for WordPress Development
  3. Working with User Settings in Visual Studio Code
  4. Writing Unit Tests with PHPUnit, Part 1: The Set Up

Once you’ve done that, return to this post and let’s continue discussing the tearDown function and what unit tests in the context of WordPress actually look like.

Continue reading

Writing Unit Tests with PHPUnit, Part 1: The Set Up

Earlier this month, we began looking at installing PHPUnit in Visual Studio Code with the ultimate goal of learning how to write unit tests for our WordPress-based projects.

To that end, this post assumes that you’ve read the following posts and it assumes that you’ve caught up with a handful of previous posts:

  1. A WordPress Development Environment (Using a Package Manager)
  2. An IDE for WordPress Development
  3. Working with User Settings in Visual Studio Code

And, of course, installing PHPUnit in Visual Studio Code as linked above. Once that’s done, we’ll be ready to proceed. But one thing to keep in mind is that this will night be a traditional or a comprehensive course in writing unit tests.

Intalling PHPUnit in Visual Studio Code: Installing the PHPUnit Extension

Instead, it’s all about writing unit tests for WordPress projects.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑