A comprehensive guideline to setup phpcs for a WordPress theme or plugin.
You need to install the following softwares on your machine.
- Navigate to the project root directory.
- Open composer.json file and add/update the codes.
"require": {
"php": ">=7.4|^8.0",
"composer/installers": "^1.0.12"
},
"require-dev": {
"wp-coding-standards/wpcs": "^3.1",
"phpcompatibility/phpcompatibility-wp": "2.1",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"squizlabs/php_codesniffer": "^3.10"
},
"config": {
"allow-plugins": {
"composer/installers": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
}
}
- Run the command to install composer packages.
composer update
- You will now see a bin folder inside the
vendor
directory.
- Next, open the
package.json
file and add this code.
"lint:php": "vendor/bin/phpcs",
"lint:php:fix": "vendor/bin/phpcbf",
- Now, copy/download the
phpcs.xml
file from this link and paste it to the project root directory.
- All done. Now, we can run this command to check the php linting errors and fix them.
npm run lint:php
npm run lint:php:fix
Output: