Is WordPress Object-Oriented? A Thorough Exploration

Here at WPShout, we’ve got some thorough tutorials on using object-orientation as a WordPress developer. We’ve hopefully convincingly explained that it’s pretty common and easy for a developer to write something when programming WordPress that is OO. Many many plugins you see are is essentially object-oriented WordPress plugins. What I want to explore today is a more esoteric question: Is WordPress an object-oriented system? Or, in short “Is WordPress OOP?”

As I’ll go into a lot more detail on, I don’t think that it’s correct (as I write this in early 2019) to say that either WordPress PHP or WordPress JavaScript are “object-oriented.” WordPress programming languages vary a little in how relevant this question is for them. But to say more about that, we must first tackle what “an object-oriented system” is. Then we can get into the specific and subtle arguments both to say either “yes” or “no” to “is WordPress object-oriented?”

What Does It Mean for PHP Code to be “Object-oriented?”

As we’ve discussed a lot, especially in our free “OOP for WP” course, “object-oriented” programming is essentially an exercise in thinking about objects. I think it’s one of the three most important programming paradigms to consider around PHP and WordPress. It’s contrasted with two others: one that thinks about “procedures” and another that thinks in terms of “pure functions.” For a ton more on that, check out this article:

The Three Development Paradigms: Procedural, Object-Oriented, and Functional

In short, having PHP objects (PHP classes, which are instantiated into objects) in a system doesn’t make the system itself “object-oriented.” While opinions differ, I’m partial to the idea that a programming systems would be accurately thought of as “object-oriented” if most of the system was orchestrated through the interaction of different objects. In WordPress, there are a lot of objects. But I still don’t think WP is OOP. But before I get ahead of myself, why objects anyway?

What Objects do For Us in PHP

Objects are essentially concepts that help fit the way human minds work. That’s the casual understanding. So lots of OOP tutorials will talk about how humans understand things like “chairs” and “dogs” as objects. And how those “chairs” and “dogs” in the real world are in some sense instances of a purer concept (or “class”) of them that we hold in our head.

This is where WordPress has lots of relevant objects. Some that quickly come to mind for me that I know without even looking are that WordPress has:

  • WP_User — which represents users with accounts on a site
  • WP_Query — which represents a database query WordPress makes for “posts”
  • WP_Widget — (possibly not long for this world), which represents a sidebar widget
  • WP_Post — which represents a WordPress post of blog content

All of these fit with a basic story one could tell that “there are objects, therefore it’s an object-oriented system.” I don’t really believe that story, but I see that told a lot.

How Most People Mean “Object Oriented”

So, like I just said, a popular definition of “an object oriented system” is “a system that uses PHP objects.” If that is our simple definition, then WordPress is certainly an object-oriented system, at least on the PHP code. But there are competing programming paradigms, and one of the them is about using “procedures” or “imperative code” and most people say WordPress does that, too. I personally think WordPress is more procedural than object-oriented.

To be an object-oriented system, I’d argue that the system must be nearly exclusively objects orchestrating each other. Almost no system is “pure” in so far as that every line of code is “object-oriented” or “in classes,” at least in the PHP and web world. But a lot of systems are much closer to “objects orchestrating objects” than WordPress is. WordPress has a lot of “global functions and variables” which I’d argue gets you a lot closer to “a procedural system with objects” than it is to “WordPress is an object-oriented system.”

A Brief History of OOP Contrarianism

Photograph of Alan Kay on stage

Alan Kay (pictured) argues about OOP ;p

I also feel somewhat obligated, because I love fringe opinions, to point out that there’s a small but significant contingent of people who argue “most of what people call object-oriented code misunderstands what object-orientation means.” This whole line of argument most prominently made by a man named Alan Kay, who some people call the inventor of object-oriented programming.

Kay’s argument can be simplified to “All object-oriented programming is about is message passing. All this object-focus misses the point that objects should never ‘call into’ other objects.” This argument is much more complex than that, but suffice it to say, I mention it because if we understand OOP the Alan Kay way, neither WordPress nor any code written in Java (one of the most obviously “OOP” languages) really fully qualified as object-oriented at all.

OOP in JS? Does WordPress do that?

A final note, because of my greater familiarity with it, and because of it being the clearer “OOP” language of the two, this article has and will mostly focus on the PHP that is written in and around WordPress. But a lot of WordPress’s real code, and a growing proportion with WordPress 5.0 and the Gutenberg editor, is written in JavaScript.

JavaScript is weird as a (WordPress) programming language. It hasn’t (and kind of even doesn’t in its most recent iteration) fit the model of “class-based object-oriented programming.” As of “ES6,” JavaScript has classes. React, one of the most popular JavaScript libraries today, and one used a lot in Gutenberg, uses these classes. So those systems could maybe be described as “object-oriented.” But as I’m no expert on either, I’ll stand aside from that debate. Though I do feel that just as with PHP, much of the JavaScript in WordPress is quite clearly procedural.

So, Is WordPress Procedural or Object-Oriented?

So, enough explaining and wavering. Is WordPress object-oriented? Or is WordPress programming more of a procedural paradigm? Or as we’ve abbreviated it, “Is WP OOP?” In short, my answer is: no, WordPress is not an object-oriented system.

The Argument for “WordPress is Object Oriented”

thumbs up to setup_postdata() The case for, as I’ve outlined at some length above but will summarize here is this: WordPress has lots of objects. Large parts of WordPress are truly just orchestrating different object together. And especially over the last ten years (of its 15 year life), WordPress has gone further and further toward orchestrating PHP objects together to do its work.

To that point, I plead no contest. Undeniably, the WP REST API, which is arguably the biggest sub-system introduced to WordPress in this millennium is object-oriented. And things like WordPress’s comment system have gotten ever more object-heavy as time goes on.

But, and this is a big but, a trend toward object-orientation doesn’t make the system itself object-oriented.

Reasons WordPress is not an Object Oriented System

At heart, WordPress is a system that was designed before the PHP programming language (or especially JavaScript) could be called properly “object-oriented” on its own. So is WordPress object-oriented? Not really.

Most PHP commentators say OOP really arrived in the language with PHP 5.3. As I write this, WordPress still supports PHP 5.2. So, short of a big rewrite (which WordPress would never do for backwards compatibility concerns), WordPress will never be an object-oriented system, properly considered. That’s my opinion and I’m sticking with it.

At heart, WordPress themes are a clear example of how WordPress can never be an object-oriented system. The WordPress template hierarchy (which we love so much we maintain means that it will just never really work in an object-first way. The WordPress template hierarchy is not object-oriented. And as a core part of how WordPress works, I think it’s as good a reason as any that WordPress shouldn’t be considered object-oriented.

Bonus: Could there be an Object-Oriented WordPress Theme?

But, could you make an object-oriented WordPress theme? Sort of. In fact, in asking this question myself I found one. It’s called “OOWP”. And if you squint a little you can see how it is both a “wordpress theme” and “object-oriented.” But I’m not sure that even its heroic efforts have made it into something I’d comfortably called “an object-oriented WordPress theme.” Partly because it’s diverts so deeply from the way “WordPress themes” are typically built and structured.

But this is the only theme I’ve ever found (and it was a result of explicit looking) that works this way. Most themes and WordPress themers are quite happy to use the WordPress template hierarchy. And I see no good reason to tell them they’re wrong or must change. But that does mean to me that “an object-oriented WordPress theme” is a bit of an oxymoron.

What it Means that WordPress Isn’t Object Oriented

For the most part, this whole article is an exercise in philosophy. You’ll notice at no point did I (nor would it have made sense to) ask if “an object-oriented WordPress” would be either a good idea or one that would lead to more developer happiness. I know many developers, myself included, who have at times thought that it would. But WordPress simply will never go there, so we’re kind of wasting time wishing for it.

At the end of the day, we do WordPress development because we love WordPress. And because that, questions about theoretical purity to this or that programming paradigm for WordPress is a complete distraction. We should just keep going out there and making cool things! Is WordPress object-oriented? In some sense it doesn’t matter. Knowing that it kind of is should lead us onward to better WordPress programming. Yee-haw! 🤠

Image credit: Marcin Wichary, Dmitry Baranovskiy

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
January 25, 2019 1:24 pm

Here’s where headless wp using the api comes in.