Coding standards are a way of improving the way you write code. They are what makes you a good developer. It is excellent to observe and practice the coding standards of any language in which you are working.

WordPress also has its coding standards. All of them are readily available at the codex. These coding standards include four languages.

  1. PHP
  2. HTML
  3. CSS
  4. JavaScript

If you haven’t gone through them already, I think you should. Because following those rules will make you write good and readable code. These rules helped me improve the quality of my code from beginners to an advanced level.

I have recently added WordPress Coding Standards into my workflow. And in this post, I am going to explain how you can use them too.

PHP_CodeSniffer

WordPress Coding Standards are a set of rules which validates the code that we have written to the standards of the Codex. They use PHP_CodeSniffer to ensure the quality of the code.

PHP_CodeSniffer is a set of PHP scripts whose job is to validate our code against a set of rules. It contains two types of scripts.

  1. phpcs
  2. phpcbf

phpcs recognizes the violations of coding standards in our code and phpcbf is what we use to fix our code against a set of standards.

Installing PHP_CodeSniffer

There are two ways to install PHP_CodeSniffer into your local environment.

You can use Composer to install PHP_CodeSniffer. For those who do not know about Composer, it is a tool for dependency management in PHP. You can read more about it here.

Enter the following command in your terminal to install Composer.

composer global require "squizlabs/php_codesniffer=*"

This command will install PHP_CodeSniffer globally in your local environment.

Installing WordPress Coding Standards

The next step is to install WordPress Coding Standards. First of all clone the official repository in your system where it is readily available. Now use this command in your terminal to clone the repository.

git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs

This command will clone the whole repo in a folder named wpcs in your present working directory (pwd). Now you just need to add the path of these rules to PHP_CodeSniffer. To complete this step, enter the following command in your terminal.

phpcs --config-set installed_paths /path/to/wpcs

To check whether the path is added to phpcs or not, use the following command.

phpcs -i

If WordPress-Core is present in the list of coding standards, then everything is fine, and you have completed the installation process.

Configuring development editors

You can use these rules in the following major development editors.

  1. PHP Storm
  2. Sublime Text
  3. Atom
  4. Visual Studio

Wrapping Up

In this post, I have shown that how you can add WordPress Coding Standards in your workflow. These standards will help you improve the quality of your code.

Are you using these standards already? Do you feel any difference in the quality of your code after using these rules? Share your feedback in the comment box below or reach out to me via Twitter @AsharIrfan.