PHPCS Code Linting for Wordpress

‘Linting’ automatically analyses your code for potential errors and ensures it’s formatted according to a definition. There are tools to lint your code across many programming language and can be customised on a per project basis to increase code quality & consistency, improved performance and reduce developer decision making fatigue.

This guide will help you setup Code Linting for your Wordpress project.

1. Installation

Using Composer — Install PHPCS and the Wordpress Coding Standards, to your local machine:

Add the Wordpress Coding Standard to the PHPCS list of available standards:

To test it’s working, from the command line, you should now be able to run:

Which will return:

2. Project Config

The first step is to setup project-specific linting configuration. There’s a couple of benefits in making it project-specific (opposed to globally defining config):

  1. The config can be shared across team members
  2. You can use different config for different projects — a Wordpress code style guide for Wordpress projects but not a Laravel project for example

In the root (or perhaps even just in your theme or plugin — depends on how much you’re writing) of your project, track the following phpcs.xml file (note that this is for Wordpress):

To lint a single file through the Wordpress Code Standards — eg. the header.php, you can now run:

For me, this returned the follow error because I had a function like so: function foo($bar) however Wordpress Standards want it like so: function foo( $bar )

3. Realtime Feedback

PHPCS is now working in our terminal, however the real benefit comes from error and standards checking in realtime. We can setup our IDEs to give us this immediate feedback as we code.

3.1 VS Code

There are plugins/extensions for most IDEs, I’ll highlight how to set it up in Visual Studio Code (VSCode):

  1. Install the VSCode PHPCS extension
  2. Open settings.json (CMD + Shift+ P) and add the following to the settings object (replacing <absolute/path/to> with your own path):



3.2 Sublime Text

  1. Install the SublimeLinter and SublimeLinter-phpcs extensions
  2. Open Preferences.sublime-settings (CMD + ,) and add the following to the settings object (you can use which phpcs to find the path to PHPCS):



From there, you can use the command palette (CMD + p) to “show all errors” and then “lint this view”. I personally edit the SublimeLinter’s settings, to make the linting happen in the background (“lint_mode”: “background”).

I lead, design & build digital goods @wearepvtl

I lead, design & build digital goods @wearepvtl