The longer you work with a given platform, the more you begin to recognize certain ways to do something. And it’s very likely that you’ll begin to recognize two things these things can be done:
- in a way that’s fast to develop, but harder to maintain or
- in a way that’s slower to develop, but easier to maintain.
Of course, like any project, requirements, constraints, budgets, and time are all going to impact what you’re able to implement. But what I’m getting at is that regardless of how you implement a solution, you begin to get familiar for how to solve a given problem.
If a problem has to be solved quickly, then you know the tradeoffs, but you likely know how to do it quickly. Similarly, if you have a little more time (and budget), you can develop it another way, and you’ll know how to do it and possibly even wrap it up with test suites if the constraints allow for that.
Regardless of how you have to implement a solution, you know the ways that you can do it.
And this raises a question:
Are there design patterns specific to a given platform? And if so, what are some of the design patterns that may be available in WordPress?
Design Patterns for WordPress
Of course, I’m not talking about design patterns the Gang of Four developed so many decades ago. Instead, I’m talking about ways to do certain things in WordPress.
- properly sanitizing information before writing it to the database (for posts, taxonomies, metadata, and so on),
- reading data from the database using the WordPress database utility (or $wpdb),
- validating and returning information from the database when requested,
- verifying the security of an Ajax request before doing any work,
- properly setting up a custom query and then resetting it so not to interfere with the main loop,
- adding page-specific assets in both the administrative area of the WordPress and the public-facing site of WordPress,
- and so on.
There are plenty of things that we could list here. But I think it’s worth exploring what some of these may look like.
First, though, what are design patterns in the classical sense?
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, most notably computer science.
I’ve worked in WordPress long enough and built enough plugins and tools using the application to recognize how to do certain things versus coming up with something original for every single project.
Is it safe to say, then, that there are re-usable things we can employ in WordPress for common problems? Sure. They’d need to be generalized, of course.
So I’m going to take a middle ground on this at least for the sake of blogging. I’m going to start a series that will talk through the above topics along with code samples.
They are going to be short articles, and I’ll do what I can to make sure it’s easy to follow (with code comments and explanations) and, who knows, perhaps this will grow into something more.
I’m Not Talking About Libraries
This isn’t to be confused with libraries. Remember, libraries are like small pieces of software that we may be able to run independently, or they may be a suite of tools or functionality that can be imported into your project to add functionality.
In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subroutines, classes, values or type specifications.
Maybe certain WordPress-specific patterns could be built into their libraries, but that’s not the point of the post.
Instead, I’m simply trying to lay out potential, common patterns that exist in WordPress.