Part of WordPress’ success stems from how easy it is for new developers to contribute to the platform. However, on this type of collaborative project it’s particularly important for coding styles to remain consistent. Without set guidelines, the code can quickly become unmanageable.

The WordPress PHP coding standards emerged to help address this problem. Following these standards results in code that is easier for contributors to read, update, and manage. As an independent developer, learning standards like these will help you write stronger code as well.

In this article, we’ll introduce you to the WordPress PHP coding standards and explain why they are so important. Then we will discuss three of the standards in greater depth, to help you understand why and how some of these rules are made.

An Introduction to WordPress PHP Coding Standards

In programming, there is rarely only one way to write code. Because of this, code written by two different programmers may be extraordinarily different. That’s why some consistency is helpful in a collaborative project like WordPress, so as many people as possible can read, write, and understand the code. As a well-known phrase from the Perl programming community states: “There’s more than one way to do it, but sometimes consistency is not a bad thing either.”

Coding standards are a simple way to bring together a project where contributors have different styles and levels of experience. WordPress published their PHP coding standards for this reason:

WordPress PHP Coding Standards

The PHP coding standards help create consistency between programmers, and offer newer members a list of best practices to follow. If you contribute to the WordPress core or submit plugins and themes to the directories, your code needs to follow these standards. However, we find that it is also good practice to use them for any WordPress-related work you do. After all, following established standards is very helpful to anyone else who may need to work with your code one day.

3 Essential WordPress PHP Coding Standards

Many of the WordPress PHP Coding Standards are simple and straightforward. They are important because too often ‘done right’ simply means ‘it works for me (on my local machine)’. Set standards offer a concrete way of knowing whether code is up to snuff and will work across all machines and circumstances.

1. Choose Single or Double Quotes to Prevent Escapes

There are two main ways to write strings in PHP. You can use either single or double quotes around your chosen text. Single quotes interpret everything within them literally, while double quotes check for variables and attempt to show their values:

$example = 'Bob';

echo 'Single quotes. $example says hi!';
// Output: Single quotes. $example says hi!

echo "Double quotes. $example says hi!";
// Output: Double quotes. Bob says hi!

What happens when you need to include quotes within your string? If the quotes within the string match the outer quotes, you have to escape them using the backslash. Similarly, if you want a variable to show its value in single quotes, you have to concatenate it with the rest of the string:

echo 'This is a string with \'escaped single quotes\' in it.';
// Output: This is a string with 'escaped single quotes' in it.

echo 'This is a string with ' . $example . ' in it.';
// Output: This is a string with Bob in it.

How do you decide whether to use single or double quotes? WordPress standards tell you to choose double quotes if there is a variable within the string. When in doubt, choose the option with the least characters to escape.

Here are the official WordPress examples of well chosen quotes:

echo '<a target="_blank" href="/static/link" title="Yeah yeah!">Link name</a>';
echo "<a target="_blank" href='$link' title='$linktitle'>$linkname</a>";

As you can see, single quotes are used most in the first line because there are no variables. In the second line, we use double quotes to account for the variables, and single quotes for the HTML link tag.

2. Always Use Braces for Clarity

When writing an IF statement and loops in PHP, you can choose to include or ignore braces for single lines of logic. Here are a few versions of what this might look like in code:

// Block IF conditional statement with braces
if ( $condition ) {
   echo 'First line of code';
   echo 'Second line of code';
}

// Single line IF statement without braces
if ( $condition ) echo 'Single line conditional';

// Single line IF statement without braces
if ( $condition )
echo 'Single line conditional';
echo 'Second line is not part of the IF statement';

As you can see, code without braces can become confusing to read quite quickly. This is probably why WordPress chose to include braces as a must in their PHP coding standards.

When writing these blocks within the WordPress infrastructure, you should always include the braces, even for single line conditionals where PHP doesn’t force you to. This will make your code more intuitive when others are trying to edit it.

3. Avoid PHP Shorthand Tags for Distributed Code

PHP shorthand tags are an alternative way to write PHP tags:

// Normal PHP tags
<?php
<?php echo

// Shorthand for <?php and <?php echo 
<?
<?=

Some developers prefer shorthand tags because they are faster to write. However, not all servers have the module required to run PHP shorthand tags.

As WordPress is widely distributed to people with many different server configurations, shorthand tags are a big no-no. Including them could bring down the entire site of someone who doesn’t have shorthand tags installed on their hosting server. The official stance is to always use full PHP tags within WordPress projects.

Conclusion

The WordPress PHP Coding Standards acts as a guide so developers can write cleaner, easier-to-read code when contributing to WordPress’ core. By learning and following these standards, you’ll write better code that is more likely to be accepted and built upon.

To get you started, try implementing the following three WordPress PHP standards:

  1. Choose single or double quotes in order to prevent escaping.
  2. Always use braces for clarity.
  3. Avoid PHP shorthand tags, since WordPress is widely-distributed code.

What questions do you have about the WordPress PHP Coding Standards? Let us know in the comments section below!

Image credits: Émile Perron.

John Hughes

John is a blogging addict, WordPress fanatic, and a staff writer for WordCandy.

The post The Newbie Developer’s Guide to WordPress’ PHP Coding Standards appeared first on Torque.

Share This