Yesterday, I gave a primer on the repository pattern. In short, it’s one of those patterns that I think anyone working on middleware built on top of WordPress should understand.
When giving a primer on a pattern like this, it can be tough to do justice to the pattern when you need to:
- introduce it,
- explain how it works,
- cover the benefits,
- and give a small demo.
But the real advantage to the repository lies not only in abstracting the data layer away from the rest of the application but that it can (or should) be able to be easily swapped out with various data stores without changing the API.
For example, in one instance, you may need to retrieve data from the WordPress database, in other cases you may need to retrieve something from a third-party API, or perhaps there’s some other place from which you need to retrieve data.
Regardless, the idea behind the repository pattern is that whatever sits behind it doesn’t matter so long as the API it provides works for the layer of the application calling into it.
And since we’ve covered a primer on the repository pattern, let’s take a look at some of the repository pattern benefits and how we can implement it in the context of WordPress projects.

