Taking a Closer Look at WordPress Coding Standards

According to W3tech, 65.1% of websites that use a content management system operate on WordPress. It’s a renowned open-source CMS written in PHP language. Due to its diverse range of custom themes and plugins, many SaaS providers and digital marketing agencies utilize WordPress for creating websites.

Why WordPress Introduced Coding Standards?

Since WordPress is a massive open-source software,  it consists of a huge source code to run. This is due to the contribution of several developers to this date. As coding can be done in multiple styles, developers could add any kind of code they‘re familiar with to the WordPress platform. This can result in complications to WordPress.

If multiple coding styles were implemented into WordPress’s source code, the complex it would become for different software developers to read and extend the software’s applications. DevOps staff scanning through different styles of code would find it hard to identify errors. This can cause websites to crash at the time of launch, causing damage in terms of loss of customers to businesses.

To prevent such circumstances, WordPress introduced coding standards. These were a set of regulations that are compulsory for developers to follow when extending WordPress’s functionality.

Coding standards simplify the process of handling and assessing volumes of code samples. This is because they follow the same formatting styles, making it easier for different developers to understand what’s happening in the source code. Overall, coding standards help to maintain the quality of source codes contributed by other developers.

What Coding Standards Does WordPress Use?

WordPress doesn’t solely depend on the PHP language. It also utilizes HTML, CSS, and JavaScript. Thus, its coding standards vary based on these programming languages. If you’re looking forward to creating your own WordPress plugin and enhancing the WordPress platform, here are the coding standards that you need to follow while creating your code.

1. Coding with PHP

For the contribution of uniform, clean, and understandable coding, WordPress introduced coding standards for PHP markup. If you’re making any WordPress projects through PHP markup, follow the following coding regulations.

  • Use of Quotations

WordPress recommends using double and single quotation marks at appropriate conditions. Single quotes can be used inside a string when nothing is evaluated. When using quotation marks, reserve one quotation style for use inside a string and the other for enclosing the whole string.

Escaping quotes in a string isn’t encouraged.

  • Indentation

When using indentations, WordPress emphasizes using tab keys rather than spacebar keys for denoting indented lines of code. The advantage of this is that your code becomes compatible with different editors that are using it on different platforms.

Indenting helps logical areas of your code to stand out, making them clear and understandable

If you’re using associative arrays and it contains multiple items, every item of that array should have its own new line. At the end of the last item of your array, add a comma. This makes it easier for adding new elements into the array or for changing its order.

  • PHP tags

When using PHP code snippets in HTML blocks, the PHP open and close tags should be on a separate line by themselves.

WordPress forbids using shorthand PHP tags as they can cause problems while running your code.

  • Braces

WordPress makes it compulsory to use braces in your code. An opening brace has to be placed in the line of the beginning statement. Braces are omitted only for single-line statements. Loop statements must also include braces. If multi-block statements are being used, then braces are necessary.

  • Usage of Space

Space must be put on both sides of assignment, logical, comparison, and string operators and after commas. Space must be also put after opening and before closing parentheses, except for typecasts. Trailing whitespaces at the end of every line of code must be deleted.

  • SQL statements

WordPress’s coding standards encourage writing SQL statements in uppercase. These statements can be written in a single line as long as they’re short and easy to read. For complex SQL queries, it advises to divide them into multiple lines, indenting each SQL statement.

  • Naming Principles

Developers are required to follow WordPress regulations while naming functions, classes, and files to prevent messy errors. When creating functions or variables, names must be written in lower case, and camelCase is strictly prohibited. If multiple words are used while naming them, separate them with an underscore.

Similar is the case for filenames, except that if multiple words are used, they must be separated by a hyphen. If your file consists of a class, then the filename must be written in the format class–class name in lowercase with every underscore replaced by a hyphen.

If files consisting of template tags are being read or added in the wp-includes folder, -template must be appended to that file name.

When creating classes, class names have to be capitalized. If multiple words are used, they should be separated by an underscore, and each word needs to be capitalized.

2. Coding with HTML

Coding regulations state that HTML code snippets have to be run through the W3C validator before the manual checkup of the code. This helps to spot and fix any markup errors in your code. The following standards have to be followed when adding HTML coding.

  • Self Closing Tags

Self-closing tags in HTML need to be handled with care, especially with spacing. W3C instructs that space must precede the self-closing forward slash when using self-closing tags.

  • Tags and Attributes

Tags help to design the structure of an HTML page. They surround the web contain and enable the browser to determine the kind of content it is. Attributes provides additional details about HTML elements.

WordPress’s coding regulations advise that all HTML tags and attributes have to be written in lowercase. Attribute values must also be written in lowercase for machine interpretation. In the case of making data human-readable, proper capitalization can be used.

  • Quotations

Double or single quotation marks are necessary to use when writing attribute values in XHTML. WordPress’s coding standards for HTML advise the same format for preventing any security issues in the HTML code.

3. Coding with CSS

CSS is another essential programming language for designing the layout and theme of your web pages. WordPress introduced coding standards for CSS coding so that developers could make consistent, clean, and readable code for projects. Here are the recommended guidelines.

  • Structure

 CSS provides many options for designing your style sheet. However, WordPress provides basic guidelines in CSS for making clear and distinct code. This helps other developers collaborate with you to understand your document’s flow.

The guideline advises using tabs for indenting every property used in your code. Coding blocks belonging to the same portion must have one blank-line space between every block. For differentiating coding pieces, 2 blank lines must be added between each coding piece.

If you’re listing multiple selectors for one ruleset, each selector must be written in a separate line. For property-value pairs, each should have its own line, use one tab of indentation, and end with a semicolon. When using only one selector, an opening brace must be placed in that selector’s line.

Otherwise, the opening brace should be added in the last selector’s line, and the closing brace should be placed in a separate line at the same indentation as the opening selector.

  • Selectors

The coding standard for selectors is similar to PHP coding standards for filenames. While naming selectors, they have to be written in lowercase, and hyphens must be used for separating multiple words. The use of camelCase and underscores is prohibited.

Selector names should be able to give a brief description of what elements they style. These names should be human-readable. For attribute selectors, double quotes must be placed around their values.

  • Properties

WordPress discourages using specific properties that can cause the flexibility of the design to decrease. Its coding standard advises that each property needs to be followed by a colon and a space.

Properties and values have to be written in lowercase, except for font names. For adding colors, hex codes or rgba() is preferred, and lowercase letters must be used. Usage of shorthand properties is advised for background, border, font, list-style, margin, and padding values.

4. Coding with JavaScript

JavaScript is an essential programming language for creating WordPress applications. WordPress formulated coding regulations so that developers can bring consistency while styling JavaScript code. Few of these coding standards are mentioned below.

  • Refactoring Source Code

Due to inconsistencies present in WordPress’s code structure for JavaScript, refactoring source codes was important for keeping it updated. WordPress is gradually resolving this issue. It recommends that developers who are working on JavaScript code follow the current necessary guidelines. Whereas for older JavaScript files, refactoring isn’t necessary.

  • Use of Spacing

WordPress recommends using spacings appropriately for enhancing the readability of your source code, making it optimized for browsers to process it.

The coding regulations endorse that tab must be used for indentation.  Whitespace must be eliminated on blank lines and at the end of lines. Every line of code shouldn’t exceed 80 characters. Braces must be used for if/for/try blocks and should be entered on multiple lines.

Conclusion

The objective of coding standards is to help developers form legible code. The cleaner the code looks, the easier it is for others to understand and implement. WordPress is an open-source application that is supported through the contributions of a growing community of supporters.

So, while creating your WordPress project, remember to follow the coding regulations so that you can contribute quality coding.