Beginner’s Guide To WordPress Coding Standards: PHP

By May 7, 2015 Blog No Comments
Beginner's Guide To WordPress Coding Standards: PHP

When it comes to developing great premium WordPress themes, you not only have to be creative and innovative to come up with a design that grabs the attention of your potential buyers, but you also have to be just as good at coding. Even the world’s visually most appealing theme won’t do much for you, unless the code is structured, well-organized and thoroughly commented so your buyers and other developers can easily understand it. If you want to find out more about the WordPress coding standards, check out these tips for writing the perfect PHP code.

Why We Need WordPress Coding Standards

All developers have their own unique styles of coding that make perfect sense to them. However, when working on things like WordPress themes, your ultimate goal is a theme that sells, which is why you have to follow WordPress coding standards. If everything goes well, hundreds, if not even thousands of people will be going through your code and you want to make sure your code is clear, and easy to follow and customize. It’s not only your buyers that are looking for a theme with well-written code, many theme markets require you follow the WordPress coding standards in order to sell your theme.

Beginner's Guide To WordPress Coding Standards: PHPBuy Theme Open Demo

The main programming and markup languages required to develop WordPress themes are CSS, JavaScript, HTML and PHP, but today we’ll focus on the most important WordPress coding standards when it comes to writing the PHP code.

Single And Double Quotes

There are four different ways to specify strings in PHP, but most commonly used methods are single and double quotes, and it’s vitally important you understand the difference between the two. Single quotes are used when you want the string to be just as it was written. The string inside the single quotes will not be evaluated, while a the string inside the double quotes will. Single quote are generally faster, because everything quoted inside is treated as a plain string, and PHP won’t use additional processing to interpret what is inside the single quote.

For example, the code

$something="Oh something";
echo "My answer is $something.

will return My answer is Oh something, because the variable $something is interpreted by PHP. However, had we used single quotes, the result would’ve been My answer is $something.

Beginner's Guide To WordPress Coding Standards: PHPBuy Theme Open Demo

Brace Style

The WordPress coding standards are rather simple when it comes to using braces. You don’t have to use braces in single line blocks, while multiline blocks should always include braces. If you end up with a really long conditional, it’s a good idea to check if you can break the block into two or more shorter functions. Turning a large block of a code into its own, specialized function will help you refactor the conditional so that it’s easier to read and simpler to understand.

Naming Conventions

One of the most important rules when it comes to naming is making the names self-explanatory to ensure everyone is able to understand your code. For example, use lowercase verbs with underscores to name functions, capitalized nouns for class names, and make sure attributes, variables, and arguments explain the purpose that they serve within the class or function in which they are to be defined.

Leave a Reply