Often we hear that WordPress is a piece of crap or a technology from the dark ages from PHP.
This is not at all a deserved reputation and it is as much unfair as the bad reputation from PHP in most developers minds.
However even if the reputation of PHP is due to an old shape of the language that was not intended to be a programming language but more scripting one, for WordPress the problem is different.
WordPress’ bad reputation comes from its community itself that is still using outdated tools.
Today modern PHP development is possible with WordPress and there are tools for that.
In this article I will cover the most important from them but maybe first we need to define what is a modern PHP development as it is something that changes from one person to another.
Here we will consider a modern way to automate the maximum of actions during the development process, try to make bugs show up as soon as possible and respect standards to make the code as easy as possible to maintain.
As you may notice most tools needed in a modern development process are common to all PHP technologies and some like Github Actions are even common to most programming languages. That is why we won’t talk about these tools in this article as they are not specific to WordPress.
In that context your first mission is to create an environment easily reconfigurable.
For that we’re gonna use Bedrock, a WordPress installation slightly different from the vanilla one.
The environment is in fact a composer project wrapping a WordPress project and controlling it.
This allows some interesting features such as plugin installation with composer require or hiding confidential information like wp-config.wp
.
It also allows the composer to install the project with the CLI and use environment variables to configure your WordPress:
composer create-project roots/bedrock
If you are interested in this environment, you can check here.
Even if composer is at the base from all modern PHP projects today in WordPress it creates a problem due to the fact multiple packages can require the same dependency with a different version. This prevents one or multiple plugins to work or simply with Bedrock to be installed.
To prevent this problem a library called Mozart that encapsulates all dependencies inside your plugin namespace has been developed.
If you are interested by this library, you can get more information here.
Another big pillar from modern PHP development is TDD and the main problem from WordPress when testing is global functions defined by WordPress core that prevent all testing to be done.
The only way to bypass this problem is to install a library to mock functions as this functionality is not in PHPUnit.
The most practical library to mock these functions is Brain Monkey that mock functions with the interface from the famous package Mockery.
This allows you to mock your function in one line which makes it really easy to handle a filter or an action in a test.
If you are interested in testing your code, this is a link to Brain Monkey.
Another problem when developing a plugin is to have a code coherence through an all team.
For fixing that problem a linter is the tool you need.
A linter is a program that will open all of your code source and check if the code conforms to certain layout rules that you have set.
If it is not the case the linter is gonna report it and if possible fix it.
In PHP the most famous linter is PHPCS and it is widely used throughout the community.
However it is less known that a list of rules preconfigured to match WordPress ones exists and can make your code match the standards from this community.
These standards are available here.
Now that we are interested in the backend from WordPress, what about the interface?
We evolved for a long time from jQuery and basic CSS.
However configuring a node project just for a small plugin can sometimes look a bit over engineering.
That’s why Bud.js has been created to simplify this process but keep all advantages from a node project such as optimization from JS or the possibility to test your code easily.
Laisser un commentaire