Modern object-oriented PHP in WordPress plugin development

Search for a command to run...

No comments yet. Be the first to comment.
After the previous article in this series was very technical, today we'll take a breather and indulge in a slightly more theoretical piece. Our topic is Hook-Driven-Development and why you too should implement it in your plugins. In WordPress plugin ...

In this article, I will discuss how and why WordPress hooks are used in plugins, and how we will use PHP attributes for registering hooks in classes effectively and clearly. What are hooks and how do they work? Since I assume a certain level of knowl...

In the previous article, we already learned a lot about coding standards, and today I like to discuss three more points: the file .editorconfig, the documentation of your code, and the declaration of strict types. The file .editorconfig The .editorco...

In the last article, I explained the use of namespaces, which we need today to establish autoloading, which simultaneously defines our file and folder structure. Declare classes in their own PHP files To keep our code clear, we distribute the code se...

In this article I'll point out, what's the reason for using namespaces in our WordPress plugins and what namespaces are. We will learn, that namespaces are important for the upcoming topics in this series. The problem with naming constants, functions...

In this article series, I will present methods for building WordPress plugins in a modern and object-oriented way. The focus will be on code quality, reusability, maintainability, and extensibility of plugins and plugin code.
In this first article, I would like to briefly introduce myself and explain why I’m writing this series. I also want to highlight when these approaches and methods are unsuitable and provide additional considerations when considering modern PHP and WordPress plugins.
My Name is Marcus and at the time of publication, I’m 47 years old, living in Cologne with my wife and two kids. I have been professionally developing websites and web apps using PHP since 1999, and since 2005 using WordPress too. During this time, I have worked on large-scale web application projects using Yii2, Symfony, and Laravel. Furthermore, I have developed over 150 custom WordPress themes and more than 50 different plugins during this time. These themes and plugins were mainly commissioned by clients, including some very extensive plugins (with over 100 PHP files) that are installed on many client sites. Since then, I have enjoyed thinking and learning about plugin architecture and software architecture in general, always striving to improve my code.
There are many good tutorials and articles on plugin development available on the internet, but most of them end just where it becomes interesting for developing more complex and larger plugins. Unfortunately, there are very few resources available on code organization, file structure, and advanced PHP programming in WordPress plugins.
That’s why I developed the desire to gather all the knowledge I have acquired so far. In one place and as comprehensively as possible.
WordPress itself is not really built with object-oriented programming and does not use modern PHP due to backward compatibility. However, this does not mean that new plugins must follow this philosophy. If you are developing plugins for the WordPress Plugin Repository and want to reach the widest possible audience of users, it may be advisable to avoid certain PHP features that were introduced later. It is also essential to explicitly specify the PHP version in the plugin header so that plugin activation is prevented on WordPress installations with incompatible PHP versions. For example: Requires PHP: 8.1.
So yes, if you know what you’re doing, you can use modern PHP in your plugins. Especially if you have control over who receives your plugins (for example, in commissioned work) or even manage the hosting for your clients, there is absolutely nothing against it.
No, please don’t. Not every plugin needs those things. Especially smaller plugins with a few lines of code and just a few functions are better developed without these advanced methods. It’s not necessary to generate more overhead as needed for a project. Plugins that register only two or three hooks, haven’t to use autoloading, PHP attributes, and dependency injection (more on this in the following articles).
Although I am a big fan of Gutenberg and block themes, and developing custom blocks is one of my favorite topics, PHP remains relevant for extensive plugins. Therefore, this article series is also of long-term importance.
I may also write some articles on block development after this series. However, for now, my focus is on how modern PHP techniques can be effectively used in WordPress plugins.
In this article series, the focus is on efficiently using hooks and implementing them with modern PHP. Through many practical code examples, we will explore topics such as dependency injection, its importance, and implementation in WordPress plugins, as well as methods for unit testing and its implementation in the WordPress context.
I will strive to regularly publish new articles and – as far as my job and family allow – to already have many articles completed before the first article goes online. 😅
You should already have some experience with WordPress development and want to take the next step. You should also have heard of object-oriented programming before.