Every programming language has its own rules and regulations for the coding. These rules and regulations are known as coding standards.These coding standards are required to be followed by the programmers for adapting the best practices while developing. Coding standards are not required to be followed exactly. They are mostly conventions that can help a developer in improving their coding style.
Why do we need coding standards? What purpose do they serve?
The major purpose of the WordPress Coding Standards is to be able to develop a base for collaboration and review. These standards are helpful in avoiding common errors. Even the modification process is simplified and the readability of the code is improved. It also makes sure that the files from a project look as if they are created by the same person. It allows anyone to understand your code and modify it further.
WordPress Coding Standards For PHP
1. Single & Double Quotes
As a WordPress developer, you must know the exact use of single and double quotes. Anything that is not evaluated in the string, make use of single quotes. Go for double quotes for the string value at the time of writing PHP code.
Now, it is important to know how to make the proper use of indentation. It shows the logical structure of your code. Make use of tabs for indentation as that will improve readability. You can use space when there is a block that is more readable in case the code is written in an aligned manner. You must start displaying each item on a new line. The tabs must be used at the beginning of the line. While space can be used for midline alignment.
3. Brace Style
You must use braces for all the blocks. You can break it into two if the block is long. This improves readability and reduces complexity. Braces are used in order to avoid single-statement inline control structures. For a control structure, you can alternative syntax.
4. Multi-line Function Calls
When the function call is split over the multiple lines, each parameter should be put on a separate line. The parameters must not consume more than one line. A variable must be assigned multiline parameter values.
5. Opening & Closing PHP Tags
At the time of embedding multiline PHP snippets within an HTML block, the open and close tags should be presented individually on a line.
6. Remove Trailing Spaces
You must omit the PHP tag at the end of the file. Don’t forget to remove the trailing whitespace, if you use a tag.
7. Proper Usage Of Space
Always put space after every comma. Put spaces on both sides opening and closing parentheses of blocks. When you refer to array items, it is important to include space around the index. You should not add space before the colon on the return type declaration.
8. Database Queries
Do not touch the database directly. Make use of the functions for extracting the data whenever possible. It helps you keep your code forward-compatible.
9. Naming Conventions
Remember to use lowercase for a variable, action/filter, and function names. Make use of underscore to separate the words. Capitalize the words for a class name. The constants should use upper-case.
10. Self-Explanatory Flag Values
Make use of true & false for the string values at the time of calling a function.
WordPress Coding Standards For HTML
Use W3C Validator to make sure that markup is well-formed. It is more effective than manual code review.
2. Self-Enclosing Elements
Close all the tags properly. For self-enclosing tags, use the forward slash.
3. Attributes & Tags
Write the tags and their attributes in lowercase. Keep the attribute values in lowercase.
As per the W3C specification for XHTML, All the attributes must have value. They must be wrapped in single or double quotes. But in HTML all attributes do not need to have values. So, they do not have to be quoted.
WordPress Coding Standards For CSS
Developers who have been working for long enough in a WordPress web development company know that there are many methods for structuring the CSS. It is important to retain a high degree of readability at the time of using CSS. Make use of tabs and not spaces for indentation. Each selector should be on one line. It should either end with a comma or a closing braces.
Selectors help you in becoming efficient. There are location-specific selectors that help you save plenty of time. Make use of lowercase and use a hyphen to separate the words. Make use of human-readable selectors. Use double-quotes for attribute selectors.
The properties should be followed by a colon & space. The properties & its values need to be in lowercase.
4. Vendor Prefix
WordPress uses Autofixer as a pre-commit tool. It helps in managing the browser specific prefix. Make use of tabs and not spaces when it comes to indentation.
Put space before and after the colon, end value with a semicolon, single quotes over double quotes, make the line height unit-less.
6. Media Queries
Always At the end of any CSS, keep the media queries grouped by media and set rules for media queries.
WordPress Coding Standards For JS
If the object declaration is short, it can be made on a single line. But if it’s too long, write one property per line.
2. Array & Function Calls
The elements & arguments must have extra spaces around them.
Make use of semicolons and do not rely on Automatic Semicolon Insertion (ASI).
4. Indentation & Line Break
Indentation & line breaks will help you make your code more readable.
5. Naming Conventions
The names of the variables and function should be in camelcase. The first letter needs to be in lowercase. All the names need to be descriptive.
Always use strict equality checks. Do not use abstract equality checks.
7. Switch Statement
At the time of using a switch statement, each case barring default must be given a break.
Wrapping things up
Each programming language comes with its own set of coding standards. These coding standards must be followed by all the WordPress Developers and WordPress Web Development Companies for improving the readability of the code. This guide of coding standards for PHP, HTML, CSS & JS will prove to be helpful for the developers.
Roma Kapadiya is a Web Researcher and a SEO Consultant associated with well-known firm for Nimblechapps which is one of the best mobile application development company in India and the USA. She also likes to share the researched knowledge with the audience through engaging blogs. Her write-ups mainly feature anything and everything related to the upcoming technology, startups, programming tools and web!