WordPress and the Open-Closed Principle

Rusty Open Sign

Oftentimes we think of the SOLID principles of object-oriented programming as just that: object-oriented programming principles. However, we are going to take a closer look at the second principle, the open-closed principle, and see how it applies to WordPress.

You are probably thinking, “WordPress isn’t really object-oriented”… and you’d be mostly right. Much of WordPress is functional, although there are still plenty of objects to be found. I’m not talking about objects or entities within WordPress though. I’m talking about WordPress as a whole.

Flashback

I remember the first time I installed Magento. It took me a while and certainly wasn’t as easy as WordPress’ famous 5-minute install. I also remember the first time I tried to install an extension for Magento. I had to copy a folder to a certain location, edit a core file to make sure the extension loaded, change a setting over here… wait… yes, I said it: “edit a core file”!

Once I tried out WordPress and saw how easy it was to install a plugin, I was hooked. No crazy installation instructions and no chance that I could break my entire WordPress install because I failed to properly edit a core file.

The Open-Closed Principle

Objects or entities should be open for extension, but closed for modification.

The open-closed principle basically says that an object or entity (or system) should be open for extension and closed for modification. If you think about it, this is the real beauty of WordPress’ plugin system.

  • WordPress is closed for modification.
    WordPress core is stable, backward compatible and not meant to ever have core files edited.
  • WordPress is open for extension.
    Simply install a plugin and you can easily extend WordPress to have additional functionality.

I’d dare say that the ease of installing plugins is a big part of what propelled WordPress forward in its earlier days. The ability for a user to do what previously required a developer to do was just magical. The real key here though is that a plugin would never break WordPress in an irreversible way. If the extension breaks the system, just disable it and return to a stable core.

If we take WordPress as an example, I think it is safe to say that the open-closed principle is worth implementing. Thanks to WordPress’ hook system, all the plugins we write can also benefit from this concept. All you need to do is add a few key actions or filters to your plugin to make it extensible.

In what ways have you used the open-closed principle (perhaps even inadvertently)?

Leave a Reply

Your email address will not be published. Required fields are marked *

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