WordPress Coding Standards
Overview
The purpose of the WordPress Coding Standards is to create a baseline for collaboration and review within various aspects of the WordPress open source project and community, from core code to themes to plugins.
What is PHP CodeSniffer(PHPCS)?
PHP Code Sniffer (PHPCS) is a package for syntax checking. It can check code against defined rules covering anything from whitespace through doc comments to variable naming conventions and beyond.
Prerequisite
Composer
For adding WPCS the Composer is required ( One time Installation )
- First check if composer is installed in your system by using this command:
composer
- For Installing default composer version use this command:
sudo apt install composer
- Or check the Composer installation ref link OR refer this Doc.
WPCS Repository Details
- WPCS clone command
git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs
- WPCS GIT Link
PHP CodeSniffer Details
- To globally install PHP_CodeSniffer use this below command:
composer global require "squizlabs/php_codesniffer=*"
- Other ways to install code sniffer can be found here.
Configure PHPCS for WPCS
-
Getting Composer bin directory
- To get composer bin directory use this command
composer global config bin-dir --absolute
- To get composer bin directory use this command
-
Updating Environment variable.
- Open Terminal and add
echo $PATH
- Use this command
nano ~/.bashrc
( To Edit Environment variable in Ubuntu ). - On the 2nd last line of the file add the composer bin directory as mentioned below
eg: export PATH=“/home/prdxn122/.composer/vendor/bin:$PATH”
- Add your composer bin directory instead of yellow marked directory
- Open Terminal and add
-
Saving the file
- Ctrl + X ( For exiting editor )
- Now it will ask for saving the changes press y and then enter
-
For check if your composer bin path is added successfully run this command
echo $PATH
-
Now go to your WPCS cloned folder directory
- Open the terminal and run this command
pwd
. This command will show the path of your directory - After getting the directory path, run this command
phpcs --config-set installed_paths {Your-WPCS-Path}
- After configuring phpcs for WP run this command:
phpcs -i
and check if you are getting the output as mentioned below:- The installed coding standards are
MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
- The installed coding standards are
- Open the terminal and run this command
-
Check if PHPCS is working as excepted use this command on your theme directory
phpcs --standard="WordPress" header.php
-
To check all phpcs flags use this Command:
phpcs –help
Dynamically Run Phpcs and Phpcbf command on saving the php file
-
Install Run on Save extension by emeraldwalk on Vscode
-
As Vscode setting is displayed in UI format we have to change it to JSON format
- Go To -
Vscode -> setting -> Workbench - setting Editor -> And change the Editor option to JSON
- Go To -
Validating WP Functions
-
Add PHP Intelephense Vscode extension by Ben Mewburn
-
For enabling this extension for WordPress go to Vscode -> setting -> Extension -> Intelephense -> Intelephense Stubs and click on add item and add WordPress.
-
As this function are not WordPress specific. It will give error for acf methods like get_field, have_rows.
-
For removing the above error clone this acf-pro-stubs in your document directory
-
Go to Vscode setting in JSON format add the acf-pro-stubs directory for PHP Intelephense
-
Now the ACF methods error will be gone.
Important Extension for Development
- Using acf-snippets vscode extension by Anthony Hubble
- Use below emmet/snippets for generating the acf code
- Eg: type
field:ifelse
snippet and press enter
-
field:ifelse
-
field:if
-
field:sub
-
field:image
-
field:flex (Flexible Content Field)
-
field:relationship (Relationship Field)
-
For more snippets checkout this link.
- Eg: type
- Use WordPress & ClassicPress snippets plus autocompletions by Laurence Bahiirwa: For WordPress Method autocompletions
Fixing Minor formatting issues
- For fixing Some issues in a file use this command
phpcbf --standard="WordPress" header.php
( This command will format your WP php file ) - To Fix some minor issue in all php file in a project use this command:
phpcbf -p -s -v -n . --standard=WordPress --extensions=php
Reference Link
- WPCS Installation and configuration
- WPCS Repository
- Code Sniffer
- WPCS Reporting
- Storing Reporting into a file
- Important Wordpress Extensions
- Stubs Repository
- Phpcbf