WordPress, as all development frameworks, has its own rules to follow so that each developer can easily read and understand a colleague’s code.
This is what we call “Coding Standards”. For WordPress, we have the WordPress Coding Standards better known as WPCS!These developments rules can be read here: Discover the WordPress Coding StandardsOK, we’ve got a thousand pages of docs… but I’m not gonna read all those docs while developing… couldn’t you make it easier?
Well you’re lucky because we can integrate these rules directly into our IDEs or text editors! So your code will be automatically checked during your development.
I use PHPStorm from Jetbrains every day but you should know that these rules can be used on almost any code editor.Where do I find the WPCS?
WPCS are available on Github. I will detail how I do it but there are several methods. Take your pick!
Séb method for installing WPCS
So it’s not really my method… in the sense that it’s not me who developed it… but it’s the one I’m using and that I’m going to detail!
Before installing WPCS, you need to install PHPCS… and yes, WordPress is developed in PHP and to make it simple, WPCS adds rules or modify a little the rules of the PHP Codings Standards.
Keep in mind that, as in all the WordPress ecosystem, the goal is to make development accessible and easy.
Install PHPCS
Personally, I have a partition on my Ubuntu Linux dedicated to my projects and next to the projects, I have a folder with the WPCS and one with the PHPCS.
PHPCS are also available on Github
Clone PHPCS Github repo
Go to the place where you want to download the rules then:
or
if you haven’t entered an SSH key in your GitHub account.
You should now have a folder named phpcs.
Clone WPCS Github repo
We’ll do almost the same command to clone the WPCS repository this time:
or
I reassure you, the hardest part is over…
Setting PHPStorm
Go to “File | Settings | Languages & Frameworks | PHP | Quality Tools”.
By clicking on the 3 points to the right of Configuration you can specify the path to the PHPCS. It will be necessary to navigate and select the file “phpcs” present in the directory “phpcs/bin” obtained thanks to the preceding cloning.
If everything is OK, at the bottom of the window will show the version of Code Sniffer installed.
Let’s move on to WPCS, because after all… they’re the reason we’re here!
The path to the settings is File | Settings | Editor | Inspections
the goal here is to get to the “PHP Code Sniffer Validation” menu in order to check “Installed Standard Paths” and to search after clicking on the folder on the right for the directory to be cloned with WPCS.
If all goes well… by clicking on the 2 circular arrows, you should have the choice of several WPCS… personally, I choose WordPress-Extra. then we validate by clicking OK…