This took me ages to get my head around in order to setup WordPress coding standards in VSCode for Windows. Even using the great plugin Intelephense it doesn’t allow WordPress coding conventions which differ to PSR12. I’ll outline the steps below to get this working in Windows.
- Install and download the Composer-Setp.exe
- Install PHP CodeSniffer (PHPCS) globally so it’s available for all projects.
1composer global require "squizlabs/php_codesniffer=*" - Composer will be installed under your roaming Windows profile. Create a directory called “standards” in the following location.
1C:\Users\yourusername\AppData\Roaming\Composer\vendor\bin\standards - Using Git pull the latest version of the WordPress Coding Standards into this directory
1git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs - Add the WordPress Coding Standards to the PHPCS installed paths. This will make it available to be used by the code sniffer and beautifier.
1phpcs --config-set installed_paths "C:\Users\yourusername\AppData\Roaming\Composer\vendor\bin\standards\wpcs" - You can confirm that the WordPress coding standard has been added by running the following command.
1phpcs -i - Open VSCode and download these two extensions phpcs (code sniffing) and phpcbf (code beautifying).
- Add the following configuration to the settings.json file in VSCode.
12345678"phpcs.enable": true,"phpcs.executablePath": "C:\\Users\\yourusername\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs","phpcs.standard": "WordPress","phpcbf.enable": true,"phpcbf.documentFormattingProvider": true,"phpcbf.onsave": true,"phpcbf.executablePath": "C:\\Users\\yourusername\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat","phpcbf.standard": "WordPress"
I actually prefer to switch off formatting on save and use hot keys (ALT + SHIFT + F ) instead to format. - Restart VS Code to ensure changes have been recognised.