Setting Up A PHP CodeSniffer For WordPress using Composer & VSCode

A PHP CodeSniffer is a program that helps developers keep code clean and uniform or in sync across teams. In PHP, you can easily set up a code sniffer program to help lint and watch your files while you work and ensure that you keep to standards with everyone in the team.

Let’s get started.

1. The first thing would be to ensure you have PHP, Composer, and Visual Studio Code installed on your computer. No point going forward if you don’t have those installed.

2. Install PHP CodeSniffer plugin for Visual Studio Code with the link below:

https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs

3. Next would be to install PHPCS (PHP CodeSniffer) globally on your Computer using Composer like so:

composer global require squizlabs/php_codesniffer

This ensures you can access it anywhere on your computer. To be sure you have PHPCS installed, type in the following command:

phpcs -i

You should see something like this:

The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra, WordPress-VIP, PHPCompatibility, PHPCompatibilityWP, PHPCompatibilityParagonieRandomCompat and PHPCompatibilityParagonieSodiumCompat

4. Next, go to your WP repo and run the following command to add WordPress standards via Composer, like so:

composer require --dev wp-coding-standards/wpcs

5. The next step would be to add some scripts to our composer file like so:

"scripts": {
"post-install-cmd": [
"\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
],
"post-update-cmd": [
"\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
]
}

Then run Composer Install like so:

composer install

6. Open your settings.json file in your VS code and add the following PHPCS settings like so:

"phpcs.enable": true,
"phpcs.standard": "WordPress",

Now your WP repo should be enabled with WordPress CS standards. Open any PHP file in VSCode and watch it lint as you work.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store