How to verify coding standards in WordPress -PHP

Written by: , Jul. 18, 2016

When WordPress was developed, right from the start, Matt Mullenweg, the chief programmer and soon to be CEO of Automattic, the company behind WordPress, enforced a long list of rules regarding the code writing that every core contributor have to comply to. Those rules defines how the code should appear. Here is the full list of PHP code standards:

Coding standards have immense importance in developing for WordPress and everyone can implement those when developing for WordPress. It does not matter if you are writing customized plugin for your customer or child theme.

But first let us discuss about coding standards. Why should we use standards? Basically, the code should work, right? Why does anyone should care if there is only single space between the condition clauses or if the some documentation remark should start with a capital letter and end with a full stop?

Coding standards are very common in almost every framework and system. Code that is written by well-defined rules have huge advantages over code that it isn’t.

It is much easier to read. When code is written by several programmers, or by a single programmer over long stretch of time, it become very hard to read. It look like a book with no editor that several author wrote. Sometime private method will start with underscore prefix and sometimes it is not, sometime the variable are Camel Cased and sometime not. The spaces are not consistent and it is looks like a mess.

Also, using code standards can help mitigate errors. For example, matt, in his infinite wisdom, declared that ‘Yoda condition’ to be mandatory. It means that instead of

If ( $var === true )

You should write

If ( true === $var )

Why should anyone care about that? Unless of course if you have OCD…. The answer is simple. Using Yoda condition prevents accidental placement in the condition, thing that can drive you crazy.

Using code standards make your code look like solid, professional code. Code that pass code standards test, is a quality code.

Telling your clients or your boss that you are writing by WordPress code standards is something that can help you get that contract or get this raise in the salary. But there is huge gap between saying that you are working by the standards and actually working by those.

The code standards is basically a very long list, and it is hard to remember it all. So, how do we do it? Also, how do we allow the customer\manager to verify that the code is actually written by the standards?

This is why we have PHP Static code analysis tool that allow us to scan the code that we wrote and verify that the code follow every rule in the standards.

Using phpcs is quite easy on linux or windows. First, you must install phpcs via PEAR:

$ pear install PHP_CodeSniffer

Verify the installation by

$ phpcs -i

You should get something like that:

The installed coding standards are PHPCS, Squiz, PSR2, MySource, PEAR, Zend and PSR1

This allow you to scan the PHP code by various PHP known standards. We need to add WordPress standards to phpcs tool. We will create some directory (not temporal one) an clone to it the GitHub project of phpcs WordPress dictionary:

$ cd c:\wpcs

$ git clone -b master wpcs

After that, we will add the WordPress dictionary to phpcs:

$ phpcs --config-set installed_paths c:\path\to\wpcs

If we try php –I, we will see that WordPress dictionary is in there:

$ phpcs -i
The installed coding standards are PHPCS, Squiz, PSR2, MySource, PEAR, Zend, PSR1, WordPress, WordPress-VIP, WordPress-Core, WordPress-Docs and WordPress-Extra
Running phpcs is quite easy. Go to the directory of your code and type this:

$ phpcs --standard=WordPress ./**/*.php

That’s it! You will get a report on the issues immediately:

FILE: /var/www/html/github/wp-notice/tests/bootstrap.php
2 | ERROR | [ ] Missing file doc comment
4 | ERROR | [x] First condition of a multi-line IF statement must
| | directly follow the opening parenthesis
19 | ERROR | [ ] Missing function doc comment
19 | ERROR | [ ] Function name "_manually_load_plugin" is invalid;
| | only private methods should be prefixed with an
| | underscore
21 | ERROR | [x] File is being conditionally included; use "include"
| | instead
22 | ERROR | [x] File is being conditionally included; use "include"
| | instead

Getting zero errors reports is actually the goal here. Printing this report to clients and managers can show them that you write the code by the standards and your code have actually more quality than other code.

You can running phpcs automatically, and using other tool to fix several issues automatically. You can even combine the standards with your IDE to get notified immediately. But before all this, knowing the standards and know phpcs can put you and your code in a different level of quality.

Ran is an experienced Software Developer & Mentor. Specializing in MEAN stack and LAMP stack. Developing modules and plug-ins for WordPress. Extensive knowledge in all client side technologies: JavaScript. HTML and CSS. LinkedIn:

Share this post

"How to verify coding standards in WordPress -PHP"