Jan 21, 2019

3 min read

PHPCS Code Linting for Wordpress

1. Installation

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

composer global require squizlabs/php_codesniffer wp-coding-standards/wpcs
# Windows/Mac
phpcs --config-set installed_paths ~/.composer/vendor/wp-coding-standards/wpcs
# Ubuntu
phpcs --config-set installed_paths ~/.config/composer/vendor/wp-coding-standards/wpcs
phpcs -i
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core

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
phpcs header.php
FILE: /Users/Matt/Sites/pvtl/web/app/themes/pvtl/header.php
--------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------
1 | ERR | [x] No space before closing parenthesis is prohibited
(WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceBeforeCloseParenthesis)
--------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------
Time: 243ms; Memory: 8Mb

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):
"phpcs.executablePath": "/<absolute/path/to>/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
"phpcs.standard": "./phpcs.xml",
"phpcs.executablePath": "/<absolute/path/to>/.config/composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
"phpcs.standard": "./phpcs.xml",
From the ‘Problems’ tab (CMD+Shift+P ‘View: Toggle Problems’) — you’ll see the linting errors. You’ll also notice the red underlines within the code.

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):
"SublimeLinter.linters.phpcs.executable": "~/.composer/vendor/bin/phpcs",
"SublimeLinter.linters.phpcs.executable": "~/.config/composer/vendor/bin/phpcs",

I lead, design & build digital goods @wearepvtl https://mcnam.ee

Love podcasts or audiobooks? Learn on the go with our new app.