The MAC version of this article is here.

Install PHP

Go to the official PHP website to download the PHP version that you need. In this article, we are using PHP 7.4, and the direct download link is here:
https://windows.php.net/downloads/releases/php-7.4.20-Win32-vc15-x64.zip

Once download, extract the zip into the appropriate folder. We are using c:\codes\php\php7.4 in this article. The folder looks like this:

Install Composer

Download the installer from the Composer official website. During the installation, set the PHP path to the path that you had just extract the downloaded PHP package to.

And check the ‘Add this PHP to your path?’ option so that you can run PHP command anywhere.

Install PHP Code Sniffer (PHPCS)

Open a PowerShell terminal, run command

composer global require "squizlabs/php_codesniffer=*"

The output will look like this, which shows that php_codesniffer is installed sucessfully.


Changed current directory to C:/Users/xxx/AppData/Roaming/Composer
./composer.json has been created
Running composer update squizlabs/php_codesniffer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking squizlabs/php_codesniffer (3.6.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading squizlabs/php_codesniffer (3.6.0)
  - Installing squizlabs/php_codesniffer (3.6.0): Extracting archive
Generating autoload files

Test it by running phpcs -i

The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend

Install WordPress Coding Standards (WPCS)

Use composer to install WPCS.

composer global require dealerdirect/phpcodesniffer-composer-installer --update-no-dev
composer global require wp-coding-standards/wpcs --update-no-dev

The folder looks like this:

PS c:\codes> composer global require dealerdirect/phpcodesniffer-composer-installer --update-no-dev
Changed current directory to C:/Users/xxx/AppData/Roaming/Composer
Using version ^0.7.1 for dealerdirect/phpcodesniffer-composer-installer
./composer.json has been updated
Running composer update dealerdirect/phpcodesniffer-composer-installer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking dealerdirect/phpcodesniffer-composer-installer (v0.7.1)
  - Locking squizlabs/php_codesniffer (3.6.0)
Writing lock file
Installing dependencies from lock file
Package operations: 2 installs, 0 updates, 0 removals
  - Downloading dealerdirect/phpcodesniffer-composer-installer (v0.7.1)
  - Installing squizlabs/php_codesniffer (3.6.0): Extracting archive
  - Installing dealerdirect/phpcodesniffer-composer-installer (v0.7.1): Extracting archive
Generating autoload files
PS C:\codes> composer global require wp-coding-standards/wpcs --update-no-dev
Changed current directory to C:/Users/xxx/AppData/Roaming/Composer
Using version ^2.3 for wp-coding-standards/wpcs
./composer.json has been updated
Running composer update wp-coding-standards/wpcs
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking wp-coding-standards/wpcs (2.3.0)
Writing lock file
Installing dependencies from lock file
Package operations: 1 install, 0 updates, 0 removals
  - Installing wp-coding-standards/wpcs (2.3.0): Extracting archive
Generating autoload files
PHP CodeSniffer Config installed_paths set to ../../wp-coding-standards/wpcs

As of today, 2nd June 2021, there is compatible issue with WPCS 2 and Composer 2. Above method is used instead of the standard mention mentioned in the official WPCS page.

See this issue thread for more details.

It will show that the path is added successfully.

Using config file: C:\Users\xxx\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.conf

Config value "installed_paths" added successfully

Now try it by running

phpcs --standard=WordPress --extensions=php c:\codes\phpstuff
phpcbf --standard=WordPress --extensions=php c:\codes\phpstuff

Next Step

Now we are doing with setting up PHPCS and WPCS on Windows. See this article about making the WPCS auto fix automated on Visual Studio Code.