Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Taking a closer look at WordPress Coding Standards

WordPress is a big project with a lot of developers working on it. It also has a huge amount of themes and plugins which are written by teams of developers or individual developers. When a project grows really big coding standards become more and more important. Coding standards in any project gives a similar look and feel to the complete code base which makes it much easier to maintain. People can understand the code written by some other person as it will still follow the standard. In case coding standards are followed it’s even easier to understand your code if you revisit it after some period of time. It is even easier for new developers to follow through code of the project and understand what going on.

Coding standards keeps your code well spaced out and it makes which makes it easier to track errors if any. In this article we are going to check some of the coding standards and guidelines for WordPress. Knowing and understanding these standards will help you understand the WordPress core code much better when you browse through it. You should also follow these standards when trying to write a WordPress theme or plugin or even if you planning to contribute to WordPress core.

Brace Style

Let’s begin with what are the recommended brace styles for WordPress coding. There have been always a lot of disputes of where to put braces and where one should not in many languages from c to c++ to java to php. According to the WordPress recommended standard we should place the brace on the same line as the statement and not on the next line.

Regarding braces being necessary for single line comments, WordPress standard recommends that one can do without braces for single line if statements provided the whole logical block does not have any multiline block. In case there is even one multiline block then we should use braces for all single and the multiline block.

In case of loops one should use braces irrespective if it contains a single line block or a multiline block.

Following is a function from the WordPress core and it is showing some of the details about the braces coding standard we just discussed above.

Naming Convention

Now let’s discuss about the naming conventions one should use for functions and classes in WordPress.

In case of functions and variables one should always use lower case. In case you want multiple words you should separate them via an underscore. (One should never use camelCase for functions and variables.)

In case of defining classes the class name should be capitalized. In case there are multiple words you want to use in a class name you should capitalize each of them and separate them with an underscore.

File names should have all lower case. In case of multiple words they should be separated with hyphen. For e.g.:- the file name default-constants.php. In case of files which contain classes they should start with class- followed by the name of class in lowercase with each underscore replaced with a hyphen.

For e.g.:- the file name class-wp-theme.php

In case you are adding or reading files in wp-includes, the files containing template tags should end as

–template.php for e.g.:- comment-template.php

Following in some code from the WordPress core which highlights the naming convention we just discussed.

Indentation

The WordPress standard regarding indentation is that we should use real tabs for indentation rather than spaces. This helps to maintain compatibility across various editors on different platforms. The indentation should be used to highlight the logical structure of the code which will make the code readable.

In case of associative array start each new item on new line. At the end of even the last element you should add a comma which would make it easy to change the order of elements in the array.

Following is some code in the WordPress core which highlights the things we discussed about indentation.

PHP tags

In case of PHP tags one should always use the full php tags and not use the short hand php tags. The php short hand tags are disabled on some servers and using short hand php tags might lead you into issues.

Following is some code from WordPress core showing the use of php full tag as we discussed.

Quotes

For using quotes you can either use single or double quotes in your string .As a rule when you are evaluating something you should use double quotes otherwise use single quotes. In case you want to use one type of quotes in you string you should then use the other type of quotes to enclose your string. You should not need to use the escape quotes in your string.

SQL queries

In case you are using SQL queries in your code you should try to put in one line mostly. In case that’s not possible for a complex query you should break it on multiple lines and indent the query appropriately.

The sql part in the query should always be in capital letters. One should use the $wpdb->prepare functions for adding parameters to the database query.

Following is the code from WordPress core which is showing an use of SQL statement which we discussed.

Conditions

In case of conditions you should always put the variable on the right side of the condition.  In case you mess up with the equal sign you will immediately get a php parse error and it will be easy for you to correct you error.

Following is code from the WordPress core which displays the condition as we discussed.

Conclusion

We have just seen some of the most important coding standards for WordPress in this article. These coding standards are strong guidelines to structure your code. Sometimes it might be a case that you have to divert from some of the guidelines above. In might be important to understand that why is it necessary (and it should be absolute necessary) to divert from the guideline.

In such cases you should even try to document it so that others and even you (may be after sometime) could return to that piece of code and understand why the decision of diverting from the guideline is taken. WordPress has a big community following. Keeping the same coding standards as all of them will really make your journey with WordPress really easy. So have fun while coding with WordPress.