As with any open-source project, programming within the WordPress ecosystem involves a high degree of collaboration with others. Accordingly, the team at WordPress has consolidated a collection of standards that they suggest developers adhere to when contributing to WordPress or creating third-party plugins and themes. This increases readability of your code and aids anyone who wishes to build on it.
Check out our overview of the standards below to get an idea of what you should keep in mind when beginning your next WordPress project.
The heart of WordPress is its PHP codebase. Unfortunately, the same features that make PHP so beginner-friendly, such as dynamic typing and ease of deployment, also make coding in the language a rather brittle experience. That makes the PHP coding standards the most important ones for you to keep in mind during development. Here’s a sample:
- A slick one-liner might save on character count, but it also might cause someone to scratch their head for ten minutes while they try to understand what it’s doing. Save on your fellow developers’ time instead and opt for readable code instead.
- Name variable, action and functions all in lowercase and separated_by_underscores. Class names are similar, but Capitalize_The_First_Letter. Constants should be IN_UPPERCASE. File names are in lowercase using hyphens-instead-of-underscores.
- To increase security, use built-in functions rather than queries to interact with your database.
- Always use full PHP tags, i.e. <?php ?> as opposed to <? ?>.
- Use plenty of spaces and line breaks to aid readability.
- Group your variable declarations together rather than putting them on multiple lines.
- Use camelCase with a lowercase first letter to name your variables and functions. Constructor functions are an exception; use an UppercaseFirstLetter for these class-like objects.
Stylesheets using vanilla CSS (as opposed to Sass or LESS) are simpler than the previous two languages, lacking some of the more baroque logic structures. But it’s still important to stick to WordPress’s CSS standards, especially since these files tend to be rather long and repetitious.
- Line breaks are a great way to aid future readers of your stylesheets. Separate sections using two line breaks. Also, put each selector on its own line even if they are sharing styles.
- Name selectors in lowercase and using-only-hyphens.
- Media queries at the bottom of the stylesheet, or of the containing section if it’s a very large file.
The HTML standards are complicated slightly by fact that HTML and PHP will often mingle together within the same document. As always, err on the side of readability when incorporating the HTML coding standards into your PHP files.
- Self-closing elements must always contain a terminal slash preceded by a space, i.e. <br />.
- Use lowercase for tags, accompanying attributes and any values whose purpose is to be read by a machine, as opposed to human-readable values like that of the title attribute.
- Nested PHP blocks should be indented in the same manner as any surrounding HTML code.
The WordPress platform is one of the best out there for those wishing to get started with web development or even open-source programming in general. Start off on the right foot by getting into the habit of sticking to coding standards. It will help your file contents stay organized and help others make meaningful contributions to your projects.