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
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
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
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
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?
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
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
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
So let’s talk about coupling! Coupling is a complicated problem because you can never get rid of it completely. All that you can do is control how much it spreads. And there’s more than one way to do that.
One of these ways is the dependency inversion principle. It’s one of the most important principles in object-oriented design. And it’s why it’s part of the famous SOLID design principles. (It’s the “D” in SOLID.)
The dependency inversion principle isn’t something that you see with WordPress. That’s because most WordPress developers don’t use object-oriented programming. (But it’s always a good time to start if you’re not using it!) And the dependency inversion principle only helps when you’ve been using it for a while.
But what makes the dependency inversion principle special? Why is it such almost a mandatory aspect of object-oriented design? These are good questions that you deserve an answer to.
Continue reading Dependency inversion principle and WordPress
I gave an introduction to automated WordPress deployments at WordCamp Miami 2017. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.
How do you feel when you have to update the code on a client’s WordPress site? Do you feel confident that everything will be ok? Or are you filled with dread as if you were about to play a round of Russian roulette?
For a lot of us, it’s a lot closer to the Russian roulette! We press the upload button of our favourite FTP client. We then spend the next minute refreshing the home page in our browser hoping not to see a white screen of death. (Followed by a sigh of relief when everything loads as it should!)
This is a pretty stressful way to work. You shouldn’t have to feel this way each time that you want to update the code of a WordPress site. It should be something that you can do as often as you want without worrying that you broke your site in the process.
And that’s where the idea of automated WordPress deployment comes in. As the name implies, it’s all about automating this process of updating a WordPress site. This, in turn, makes this risky process safer. (But keep in mind that this isn’t a bulletproof solution!)
Gone are the days where you’re playing Russian roulette each time that you want to make a change! You can now update your client’s WordPress site with a lot more confidence. This lets you focus on shipping bug fixes (yay bugs!) and new features.
That said, there are a lot of different ways of automating your WordPress deployments. We’ll go over what makes a successful automated WordPress deployment workflow. We’ll also look at a lot of different tools that you can use to achieve it.
Continue reading Introduction to automated WordPress deployments