Some Thoughts on Design Patterns in WordPress

A few months ago, I did a series of articles on Design Patterns in WordPress that was meant to provide an introduction to object-oriented design patterns, and how they can be used when developing projects on top of WordPress.

When it comes to working with WordPress, code is normally written one of two ways:

  1. Procedural
  2. Object-oriented

For procedural programming, think of functions.php in theme development, and for object-oriented think of plugins. This isn’t to say that procedural programming isn’t used in plugins – it often is (case in point: Markdown Code For WordPress) – but so are object-oriented techniques (see the WordPress Plugin Boilerplate).

In short, decide patterns exist for a reason: They are proven solutions to common problems that appear in software development.

Yes, they may have to be tweaked a bit for a given situation, but the definition, structure, and so on remains the same. The thing is, I don’t often see many design patterns used throughout WordPress product development.

To be clear, I’m not talking about WordPress core, I’m talking about products built on top of WordPress. Perhaps I’ve just not done enough digging or paid enough attention to some of the work that’s out there, but I think if we – as developers – spent time working on implementing design patterns in our work, projects could have much more longevity rather than devolving into spaghetti code that’s so often found in WordPress work.

Design Patterns in WordPress

So, I spent a significant portion of the first part of my career working at a large company working on .NET applications. And, like they say, the more code you write, the more you begin to notice the same problems emerging again and again.

They just take on a slightly different form. This is a perfect example of when a design pattern is likely to fit the bill.

At this point in my career, the same can be said for WordPress. There are correct ways to perform certain actions and there are ways to do things that just work.

As with any programmer, as I look back at code that I wrote even 8 – 12 months ago, I cringe. But if I look at the stuff I’ve been working on, I’m more pleased. But give it 8 – 12 months and I’ll likely cringe again.

Such is the developer’s life.

Anyway, with that said, I think that we’re also starting to see certain patterns – or at least conventions – arise in theme development with the organization of things such as Underscores. Directories are laid out a certain way, code is written a certain way, and so on.

So perhaps it’s safe to say that if it’s not patterns, at least aim for conventions.

But with all of that said, why should we – as developers – bother with using and/or evangelizing design patterns in WordPress?

1. Education

Personally, I think that WordPress provides a solid platform on which to introduce budding developers into software development.

Uber Frosh Software Developer

The application is full stack from database to the front end and there is potential to use variety of technologies. It also supports plugins (or add-ons) and gives them a feeling of what it’s like to work on a significantly larger team thanks to the open source nature.

Design patterns in WordPress can help to further educate developers on patterns that have been around for decades, why they matter, and how to use them.

2. Productivity

The more code that you write, the more likely you are to see common problems appear throughout your work.

Programmer Problems

Using design patterns makes your work more maintainable, arguably more readable, and improves the maintenance factor far more than using things such as spaghetti code.

It can also help to provide a stronger conceptual model of the architecture of a product because its fitting in to a sort of predefined architecture – or, well, ahem, a pattern – that makes it easier to keep track of the various moving pieces of the application.

3. Contributions

One of the defining factors – if not the defining factors – of open source software is the ability for anyone to introduce contributions to further develop the product.

Codercat

If said product starts off by following a defined paradigm, then it makes it significantly easier for contributions to know where new code should code, how it should functions, and how it should interact with what’s already defined.

This is a much better alternative than letting people provide patches that solve a problem, but do so in a sloppy, difficult to maintain way. Of course, the patches that are introduced like that aren’t always a result of the contributor, but the core software itself. But I digress.

The Double-Edged Sword of WordPress Development

One of the greatest strengths that WordPress has is its low-barrier of entry for anyone to get started; however, I think that this is a double-edged sword.

Yes, it’s easy to get up, running, and programming with WordPress; however, the nature of some of the languages that are used throughout make it far too easy to simply get something working rather than taking the time to educate ourselves on the WordPress-way to do it, or even the more elegant way to do it.

At any right, I think evangelizing design patterns either through implementing them more in our own work, discussing them more on our own blogs and articles, and educating others on how to use them could go along way in not only attracting other software developers, but making us better at what we do, as well.

4 Replies to “Some Thoughts on Design Patterns in WordPress”

  1. SO. MUCH. THIS.

    I wouldn’t call myself a developer. I’ve written a few themes and simple plugins but I’ve got a long road ahead of me before I reach a point where I’m comfortable calling myself a dev.

    That said, I have a lot of background doing SysAdmin stuff and the same need for patterns is clear there. Sometimes I’ll look at a server stack configuration and really wonder how it’s running at all. “OH YOU CHMOD 777’d ALL THE DIRS BECAUSE A FORUM POST TOLD YOU THAT WAS THE ONLY WAY TO FIX YOUR ISSUE?”

    Even with a low barrier to entry the fact remains that installing WordPress, even a one-click flavor install, means that we’re website owners and following these patterns is almost a responsibility, not just a nice add-on bonus. And like you said, it’s all about education.

  2. One other advantage of using a design pattern is it aides debugging….i think thats the problem of open source generally, its hard to enforce a specific technique or ways in which codes should be written, for example its way easy for a theme developer to just link stylesheets files and scripts in the header section instead of doing it the right way using wp_enqueue_…..

    1. Yeah – it’s difficult to enforce certain constraints and patterns in open source, but the same can be said in the closed source community, as well.

      After experiencing both, it ultimately comes down to you (or the team with whom you’re working) to define and enforce standards of you’re own.

      I don’t think we can all conform to the same set of rules or anything like that, plus I also know that I always have room to improve; however, I figure if we can at least educate up-and-coming developers on some of the techniques that are available, then we’re doing okay :).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.