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

Beginner’s guide to regular expressions

No programming concept frightens programmers more than regular expressions. For a lot of us, seeing code with regular expressions in it can bring a sense of dread and anxiety. We often have no idea what’s going on or how a regular expression does what it does.

That said, regular expressions are a really powerful tool. (That’s why they’re used so much.) There’s always a problem that you can solve with a regular expression around the corner. (Or you can always imagine one!) On top of that, you can use them almost anywhere.

The good news is that you can get over that fear of regular expressions! Learning the basics is often enough to solve a wide range of problems. So let’s go over them together!

Continue reading Beginner’s guide to regular expressions

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

Tips for applying to a WordCamp

Let’s talk about teaching. While not the main topic discussed on this site, it’s still a topic comes up as a topic on a pretty regular basis. That’s because it’s an important and underrated way of growing as a developer.

As a WordPress developer, one way to teach that comes up over and over is speaking at a WordCamp. If you’re not familiar with WordCamps, they’re community-run WordPress conferences. There were 115 WordCamps in 41 countries in 2016 alone.

This means that the odds are pretty good that there’s a WordCamp near you! (Those odds are even better if you’re in North America or Europe.) So all that you have to do is apply and you’ll get in. That’s how it works right!?

Well, that’s not quite true. More and more people are applying each year. I’ve been a WordCamp organizer since 2011 and we saw a record number of submissions this year. (We got 130!)

This can make it hard for you to stand out from everyone else. You’re just one submission out of many. So what can you do to tip the odds in your favour?

Continue reading Tips for applying to a WordCamp

How does a PHP application work?

Most of us are familiar with how a web application works. We know that when we enter an URL in our browser, it’ll contact a web server who’ll return some HTML. The browser will then make follow-up requests and then render the web page. (This is a simplification! But that’s the main idea.)

But what’s happening on the web server returning HTML? How is the web server getting the HTML that it returns to our browser? And, to be even more specific, how does this happen in PHP?

This isn’t something that most of us know how to answer. We know (or at least should know!) how to build a PHP website. But what happens between our PHP code and the browser is a bit of a mystery.

There’s a good chance that you’re thinking, “Well, my site loads. Why should I think twice about this?” And that’s true! But the truth is that knowing how a PHP application works can be useful.

It can affect how you design your PHP application. But it’s also important if you’re looking to improve its performance. For example, it’s hard to understand PHP application caching if you don’t understand how a PHP application works.

Continue reading How does a PHP application work?

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

WordPress for the adventurous: Options API

As WordPress developers, we often need a way to store and retrieve data. The most common reason for that is that we need to store and retrieve settings for our plugin or theme. But the reality is that there are countless of other reasons why you’d need to store and retrieve data.

So how do we do it? Well, the primary tool that we have at our disposal to do this is the options API. It’s an API that offers a way for us to store and retrieve data from the WordPress database.

It’s pretty much as indispensable as the plugin API. We often can’t build anything in WordPress without it. That’s why it’s worth looking a bit deeper at how it works.

Continue reading WordPress for the adventurous: Options API