WordPress Coding Standards are significant if you want to become a professional WordPress developer or a PHP developer. When you get into the field you’ll begin to adopt policies to follow certain standards on a per-project basis.
This guide is for how to install WordPress Coding Standards globally on Composer for Mac.
If you need to still need to install Composer globally on Mac, follow this guide I wrote earlier:
Requirements for this article:
- PHPCS must be installed on your machine via Composer, globally. Here is a guide on how to do this.
- You must be an Apple-Mac user.
- You want to use WordPress Coding Standards — because you like to make your code: clean, readable, and developer-professional.
First off, let us install the WordPress Coding Standards globally to Composer. Again, make sure you have read through the requirements for this article above, so that we can make sure we’re on the right track.
Let’s execute the following command, anywhere on your terminal:
composer global require wp-coding-standards/wpcs
This will install the WordPress Coding Standards into the global Composer.
Easy enough, that’s 50% of the work done. Let’s continue.
Next, we will configure
phpcs to recognise that
wpcs (WordPress Coding Standards) exists.
First find out where PHPCS is installed, run this command:
You should get
For the purpose of this article, USERNAME is your machine’s username.
Tip: You can find your machine USERNAME by running the following command:
Before we continue, let’s find out what we have already installed within
phpcs. Run the following command:
If you see a (blank) output, that means you have no
phpcs configs yet.
Let’s go to the next step.
Now, let’s make
You will have two options to select from:
If you have an existing
phpcs config that was displayed… then simply add the
wpcs path to that, por ehemplo:
phpcs --config-set installed_paths your_example_install_path_1,your_example_install_path_2,$HOME/.composer/vendor/wp-coding-standards/wpcs
You will have to comma-separate your install paths, in that example above they were
your_example_install_path_1,your_example_install_path_2 and then we added the
wpcs install path afterwards.
But if you do not have any configs, then it will be easier, execute the below command:
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
Remember to replace USERNAME with the username that appears on your machine, es esto terminal command
Now we will have to enable the PHPCS on our code editor.
Let us use Visual Studio Code below.
To enable PHPCS on your code editor — Visual Studio Code:
- Install the VS Code extension, called PHPCS by Ioannis Kappas.
- Once installed, open your VS Code
settings.jsonfile. This can be visited by CMD+, (comma).
- Add the following to your VS Code settings.json file:
"phpcs.standard": "WordPress-Extra", "phpcs.executablePath": "/Users/USERNAME/.composer/vendor/bin/phpcs", "phpcs.composerJsonPath": "/usr/local/bin/composer",
Please note, to replace USERNAME with your own machine’s username. Once more, you can find this by using the terminal command:
And replace this with the USERNAME that was specified in the example JSON code above.
4. Reload VS Code. And happy PHP WordPress Code Standards coding!
You can find out the available code standards that are installed in your phpcs by executing the following command:
For example, you will find the following output:
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core
This means you can use WordPress-Extra as your
"phpcs.standard" within VS Code.
Have a happy day now that you’ve got WordPress Coding Standards on your side!