Just like how WordPress follows a set of coding standards, PHP has its own coding standards. Since WordPress is primarily based on PHP, following these coding standards is a must if you want to write clean and consistent code.
You can use PHP_CodeSniffer which reviews your code against the coding standards and receive warnings incase of any violations.
This section provides the steps for installing PHPCS globally on the machine.
If you are not sure if you have installed PHPCS previously, you can check your system by running the which phpcs
command. It should either return the path of the current PHPCS installation or it would return nothing – which states that you haven’t installed phpcs before.
You can find the version of PHPCS that you are working on by executing phpcs --version
.
Assuming you have PHP and Composer already installed,
cd ~/.composercomposer global require "squizlabs/php_codesniffer=*"
If you have PHPCS already installed, you can run the following command to update it.
composer update squizlabs/php_codesniffer
And you can check the version by running
./vendor/bin/phpcs --version
Add ~/.composer/vendor/bin
to $PATH
in .bashrc
or .zshrc
and reload the terminal or open a new tab.
To direct download PHPCS
you can run the following:
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && chmod +x phpcs.phar && mv phpcs.phar /usr/local/bin/phpcs
To direct download PHPCBF
you can run the following:
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar && chmod +x phpcbf.phar && mv phpcbf.phar /usr/local/bin/phpcbf
You can also change /usr/local/bin/
to a custom path like ~/.bin
and add that custom path to $PATH
as mentioned above in the Composer Install/Update section.
You can install WPCS and VIP Coding Standards via git repo as mentioned below.
cd ~/Documents
mkdir Coding-Standards && cd $_
git clone -b main https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
git clone -b main https://github.com/Automattic/VIP-Coding-Standards.git
git clone -b master https://github.com/wimg/PHPCompatibility.git PHPCompatibility
ln -s ${PWD}/PHPCompatibility/PHPCompatibility wpcs/PHPCompatibility
ln -s ${PWD}/VIP-Coding-Standards/WordPressVIPMinimum wpcs/WordPressVIPMinimum
phpcs --config-set installed_paths ${PWD}/wpcs # remember this path
You can install it via composer using the following.
composer global require dealerdirect/phpcodesniffer-composer-installer \
automattic/vipwpcs \
phpcompatibility/php-compatibility \
phpcompatibility/phpcompatibility-wp:* --update-no-dev
Note: You won’t need to set installed_paths
when used with the composer as dealerdirect/phpcodesniffer-composer-installer will take care of it.
You should check out each GitHub repo for coding standards and see its purpose.
Try phpcs -i
, it should show the coding standards and rule sets that are available, something like – WordPress, PHPCompatibility, WordPress-Extra, WordPress-Docs, WordPress-Core.
If these rulesets still didn’t show up, you need to execute the last command phpcs --config-set...
again. (you should do this everywhere you update phpcs).
phpcs --config-show
Copy path given in installed_paths. If you don’t get that path check the above section on how to fix that.
cd {Paste here}
git pull origin main
cd PHPCompatibility
git pull origin master
composer global update automattic/vipwpcs phpcompatibility/php-compatibility phpcompatibility/phpcompatibility-wp
Note: You should update wpcs every month or so or on every release.