Tag Archives: WordPress

Getting started with continuous integration and WordPress

I gave an introduction to continuous integration with WordPress at WordCamp San Diego 2018. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.

Writing high-quality WordPress code is hard to do. It requires constant effort on our part and good self-awareness to know when we slipped up. But, if your business has any sort of success (which we all want!), you’re going to work with more and more people. And many of them are likely to touch with your code.

This is going to put a strain on your development processes. It becomes harder to maintain a certain level of code quality. And you’re no longer the only person making code changes. You’re now part of a team, and you need a way to standardize all of the things you once did on your own.

That’s goal of continuous integration. It lets you automate your different development workflows. This ensures that the quality of your code stays consistent.

Continue reading Getting started with continuous integration and WordPress

Designing a class to represent a WordPress meta box

In a previous article, we discussed how to design a class representing a WordPress admin page. This was important because almost every plugin or theme needs an admin page. But this isn’t the only common thing that plugins or themes add to the WordPress admin.

Another one of those is the meta box. If you’re not familiar with meta boxes, they’re the draggable boxes that you see on the post editing screen. It’s quite common for plugins and themes to add post-specific functionality through them.

This makes them a good topic to discuss with object-oriented programming. (Is there really a bad topic to discuss with object-oriented programming!?) And, as we’ll see, meta boxes have a lot of in common with admin pages. This means that designing a class to represent them will be a lot like what we did for admin pages.

Continue reading Designing a class to represent a WordPress meta box

Designing a system: WordPress REST API endpoints

Let’s talk about the WordPress REST API. It’s been around for a little while now, and you might have even started using it in your projects. After all, a lot of us are trying to get more into JavaScript. (Zac Gordon has built a great course if you’re looking to get into it.)

But that doesn’t mean that the standard WordPress REST API will be good enough for your project. There’s a good chance that you’ll need to extend it so that you can do more with it. And that’s done using PHP even if you’re working with JavaScript. (Sorry JavaScript lovers!)

That means that this is still a great excuse to use object-oriented programming with WordPress. (Yay!) In fact, it’s an excellent opportunity to piece different object-oriented concepts together. This will let us design a system to extend the WordPress REST API!

Continue reading Designing a system: WordPress REST API endpoints

Using dependency injection with WordPress

As you use object-oriented programming with WordPress more and more, you’re going to encounter new sets of problems. These problems aren’t as simple as the ones that you encountered when you started. But that’s also good news. It also means that you’re making progress.

One set of problems that you start to encounter as you advance in your use of object-oriented programming deal with scaling. As your code has more and more classes, it becomes a challenge to assemble these classes together. That’s why in the past we looked at how to design a class whose job was to do that.

But there are other problems that come with having a lot of classes in your code. It’s how much your classes depend on other classes. This is what we call coupling in programming. (Not just object-oriented programming.)

To help with coupling, a famous software engineer (Robert C. Martin who’s also known as Uncle Bob) created the dependency inversion principle. It helps you decouple your classes by making dependencies external to them. (There’s more to it than that. But this is one of the core elements of the dependency inversion principle.)

This reduces coupling but creates a new problem as well. That problem is that you can’t initialize objects inside your class anymore. You have to pass them to your own object through its constructor or some other method like a setter method.

This makes it even harder to assemble your classes together. This is why software engineers created dependency injection. It’s a programming technique that helps solve this problem.

Most web application frameworks use it in some form or another. But that’s not the case with WordPress since a lot of its code isn’t object-oriented. That said, if you want to build an object-oriented plugin for WordPress, it’s pretty much mandatory. Lucky for us, it’s not that hard to use dependency injection in your own code!

Continue reading Using dependency injection with WordPress

Designing a class representing a WordPress admin page

A WordPress plugin can have a lot of different components. For example, some might need to use shortcodes to achieve their purpose. While others might need to create custom post types.

But one component that almost every plugin needs is an admin page. (Or they might even need more than one!) That said, designing an admin page using object-oriented programming isn’t that straightforward. There are a lot of different moving pieces that you have to take in consideration in your design.

A well-designed admin page class will combine these different moving pieces into a cohesive class. The key to achieving this is to understand the role of these different moving pieces in the larger picture of an admin page. We’ll do that by analyzing what makes up an admin page in the first place.

Continue reading Designing a class representing a WordPress admin page

How to use placeholders for WordPress translations

Have you ever worked on localization with other web frameworks? If so, you might be familiar with the idea of using keys to identify translation strings. These keys are how your translation system finds the localized text to use inside your application.

If you’ve been doing localization with WordPress, you know that it doesn’t work that way. With WordPress, you use the original (usually English) content string as your translation key. This can be convenient because you always know what the original string was.

That said, it’s still possible to do WordPress localization using keys instead content strings. This can be useful if you come from these different programming backgrounds. You might like to keep working the way you’re used to. Let’s look at how you can do that!

Continue reading How to use placeholders for WordPress translations

Designing classes for the WordPress options API

Much like the plugin API, it’s almost impossible to build a plugin without using the options API. This can be a problem if you’re trying to learn object-oriented programming with WordPress. You need a way to build a class around it.

Lucky for us, this is a lot less complicated to do than with the plugin API. The options API is really nothing more than an API around a data store. Our job as designers is simple. We need to design a class that mirrors this API.

That said, we don’t have to limit ourselves to just copying the options API as is. We can also push things further by adding some extra functionality around it. This is what makes designing a class around the options API so interesting.

Continue reading Designing classes for the WordPress options API

Using the static factory method pattern with WordPress

If you use object-oriented programming, there’s a good chance that you’re familiar with the constructor. It’s this special method that an object-oriented programming language uses to initialize a new object. In PHP (and a lot of other object-oriented programming languages), the __construct method is a special method reserved for the role of constructor.

The role of the constructor isn’t always well understood. This is especially true when you’re new to object-oriented programming. You know the method exists, but you’re not too sure how to use it.

This can lead to some less than ideal practices around constructors. This is especially true in the WordPress world where two such practices are popular. First, there’s the habit of putting hooks in the constructor. And, because of that habit, WordPress developers tend to overuse the singleton pattern.

These two practices show a misunderstanding of the role that a constructor plays inside a class. This isn’t how object-oriented programming wants you to use constructors. There are other ways to solve those problems without sacrificing the role of a constructor.

Now, this isn’t to say that constructors don’t have any problems either. They do! In fact, that’s why we’re looking at the static factory method pattern. It solves one common problem that constructors have in PHP.

Continue reading Using the static factory method pattern with WordPress

How to troubleshoot WordPress performance

I spoke at WordCamp Halifax 2017 on how to troubleshoot the performance of a WordPress site. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.

Have you ever had a client come up to you and ask why a WordPress page was slow to load? We’ve all had that happen at some point or another. Especially nowadays with the increased importance of page load times! (Everything has to be fast fast fast!)

But what do you do when someone asks you to fix such an issue? It can feel like you’re looking for a needle in a haystack. The issue could be anywhere. (It really can!)

So what can you do about it? Well, there are a lot of tools at your disposal. They can help you figure out what’s going on with the performance of your WordPress site.

That said, it’s one thing to know that these tools exist. It’s another one to be able to use them and interpret what they’re telling you. The problem is that it’s what you need to get to the root of a WordPress performance issue.

Continue reading How to troubleshoot WordPress performance

Designing a class to generate HTML content in WordPress

As WordPress developers, we often have to generate HTML content for our plugins. (And, if you’re building a theme, that’s pretty much mandatory!) But generating HTML inside a function or method is messy. You have to close your PHP tag, write your HTML, reopen your PHP tag and so on.

This combination of HTML and code isn’t ideal. You’ve coupled your templating to your code in a way that often doesn’t scale well. That’s why it’s common to see such functions or methods grow out of proportion.

The common cause of that is adding conditional logic into the HTML code. This increases the complexity of the code and reduces its readability. So we’d like to avoid that if possible.

It’s worth pointing out that mixing HTML and PHP isn’t bad per se. After all, PHP is also a templating language. We just want to create a proper delimitation between what is regular PHP code and what is PHP template code.

So let’s find a solution to this problem! The twist (well it’s not really a twist for this site…) is that we’re going to use object-oriented programming to do it. We’ll design a class that generates HTML using PHP template.

Continue reading Designing a class to generate HTML content in WordPress