WordPress coding standards explained

As a WordPress developer, you need to know about WordPress coding standards.

As WordPress is an Open Source platform, contributions must fulfill the WordPress coding standards suggested by WordPress.

The standards consist of directions about syntax format, code structures, and error messages. These standards ensure contributions relevancy with basic coding benchmarks, so that people from all over the world understand the WordPress source code, contribute or change according to respective needs.

In this article, we discuss the WordPress coding standards. Before you develop WordPress themes, plugins or scripts, knowing about standards helps you meet globalized WordPress developers benchmarks.

What are WordPress coding standards?

WordPress coding standards refer to a set of rules for WordPress developers.

According to WordPress core development Blog, following WordPress coding standards ensures seamless development, avoids errors, and makes it easy to manage, contribute, and develop the WordPress platform.

Components of WordPress coding standards

WordPress runs on PHP, along with languages including HTML, CSS, and JavaScript.

WordPress coding standards include benchmarks for the above languages. That means contributions from the WordPress community should follow the standards for respective languages.

To clarify the concept, let’s consider an example.

Developers follow their style, concept, and framework for coding. A piece of code written by one is hard to understand by others. To eliminate this gap in the WordPress community, the concept of WordPress coding standards evolved, which makes it sure to instruct developers about the baseline for contribution to the WordPress platform.

WordPress coding standards ensure the following key benefits.

  • Seamless collaboration among WordPress community. The code written by one person is easily understandable by others.
  • Clean source code. WordPress coding standards suggest proper code structure and format. Also, the standards provide a seamless experience for future code contributions.
  • Fast bug fixing. Proper coding structures are easily searchable. It helps developers fix problems fast in the source code.
  • Human readable comments. WordPress coding standards instruct developers to include comments in the source code. This is helpful, especially when someone wants to explore the source code later.

Coding standards for languages used in WordPress

HTML

(1) Indentation: Your HTML code should contain a proper indentation. For example, using Tabs instead of Spaces helps you structure your HTML code. It also improves code readability.

Proper indentation

<div class=”entry-content”>
       <p>Name, Father name</p>
</div>

Improper indentation

<div class=”entry-content”>
<p>Name, Father name</p>
</div>

(2) Single or Double quotes: HTML code snippets should have proper quotes for attributes. When using attributes with values, you must enclose values within single or double quotes.

Correct

type=”text”
type=’text’

Incorrect

type=text

(3) Attributes and Tags: Always write HTML attributes and tags in lowercase. It makes a difference for machine interpretation. You can also take care of the human readability.

Machine interpretation

<meta http-equiv=”content-type” content=””text/html; charset=utf-8” />

Human readability

<a href=”http://example.com/” title=”Description Here”>Example.com</a>

(4) Close elements properly: Close all HTML tags properly. Also, take care of the spaces in self-closing tags.

Use

<br />

Instead of using

<br/>

(5) Write well-markup HTML pages: To ensure your page validation, use W3C Validator for the purpose.

CSS

(1) Structure your style sheet properly: Like other languages, WordPress coding standards instruct writing clean CSS structures.

Correct

#selector {
background: #fff;
color: #000;
}

Incorrect

#selector {
color: #000;
}
#selector { background: #fff; color: #000; }

(2) Don’t repeat properties: Along with properties, use short words for attributes.

Correct

background: #fff;

Incorrect

background: #FFFFFF;

(3) Enter values correctly: Carefully enter values. Take care of the space and semicolons.

Learn how to use and enter CSS values

(4) Best practices: Remove the old code structure before you attempt to add new for solving a problem.

Here is a list of CSS best practices for beginners.

PHP

(1) Quotes: Like HTML, take care of the single and double quotes in PHP, especially when dealing with Strings.

(2) Indentation: It does matter in programming languages. Proper indentation helps you improve your code readability.

(3) Take care of reserved words and compatibility: Use elseif instead of else if. Read about how to avoid PHP errors.

(4) Use spaces wisely: Spaces improve code readability, such as putting space after commas.

(5) Formatting SQL statements: As PHP provides functions to use SQL statements, using proper formats helps you avoid database related errors. For example, always capitalize SQL statements, such as UPDATE.

JavaScript

(1) Objects: Object declaration is important. Spaces and lines affect the object declaration. Learn how to declare the JavaScript objects.

(2) Semicolons: Never rely on inserting automated semicolons. Insert them manually in the code.

(3) Line breaks and Indentation: If you don’t know about indentation and line breaks, you don’t know how to write clean code. Such facts improve your code readability in complex statements.

(4) Multi-line statements: Use line breaks when dealing with multi-line statements.

(5) Declaring variables in JavaScript: WordPress coding standards suggest taking care of variable declaration. There are different aspects when declaring variables in JavaScript, such as declaring variables with var, const, and let.

Conclusion

WordPress is an Open Source platform. It instructs code standards for contributors and community. According to WordPress, WordPress coding standards provide a baseline for collaboration.

As a WordPress developer, pay particular attention to PHP code standards, as WordPress is mainly composed of PHP.

Not only WordPress coding standards help you contribute to WordPress but also help you improve your coding routines in general.

Also, the use of code editors can’t be overlooked. Make sure to use code editors for proper code structuring.

To learn more about WordPress coding standards, head over to WordPress official resource.

Did you find this article helpful? Let us know your thoughts. Proceed to join the conversation in the comments.

More on wpcrib

2 comments

  1. Hello Nasrumminallah,

    thanks for this great post!
    I just don’t get one part, why do you use “”?
    For me personally I use “” for my breaks. WordPress or its Themes use HTML5 so I go for the HTML5-valid version, “” is for XHTML I think. What’s your thought about that?

    Best regards,
    Felix

    1. Hi Felix,
      Thanks for reading!
      Yes I agree. It is something related to WordPress post display. 🙂

Add comment

Join discussion and make an impact. Your email address will not be published.

GDPR is going into effect on May 25, 2018. Learn more in our new GDPR section. You can also view changes to our Privacy Policy.
We use cookies to provide a personalised experience for our users.