logomichael sumner
Contact

How to Install WordPress Code Standards using Composer on Mac

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:

How to Install PHPCS using Composer Globally on Mac

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.

Install WordPress Coding Standards to the Global Composer

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:

which phpcs

You should get /Users/USERNAME/.composer/vendor/bin/phpcs

For the purpose of this article, USERNAME is your machine’s username.

Tip: You can find your machine USERNAME by running the following command:

echo $USERNAME

Before we continue, let’s find out what we have already installed within phpcs. Run the following command:

phpcs --config-show

If you see a (blank) output, that means you have no phpcs configs yet.

Let’s go to the next step.

Make PHPCS Recognise WPCS

Now, let’s make phpcs recognise wpcs exists.

You will have two options to select from:

Option 1 (1% who already use PHPCS)

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.

Option 2 (If you did not setup PHPCS configs before)

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 echo $USERNAME.

Now we will have to enable the PHPCS on our code editor.

Enable PHPCS on Visual Studio Code

Let us use Visual Studio Code below.

To enable PHPCS on your code editor — Visual Studio Code:

  1. Install the VS Code extension, called PHPCS by Ioannis Kappas.
  2. Once installed, open your VS Code settings.json file. This can be visited by CMD+, (comma).
  3. 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:

echo $USERNAME

And replace this with the USERNAME that was specified in the example JSON code above.

  1. Reload VS Code. And happy PHP WordPress Code Standards coding!

How to Select Your phpcs.standard for VS Code

You can find out the available code standards that are installed in your phpcs by executing the following command:

phpcs -i

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!