The WordPress Coding Standards: An Introduction

We often talk about how important it is to write quality code when it comes to creating WordPress products. However, that isn’t just about professionalism and pride. Practically any site can break due to poor-quality code, which could do serious damage to your professional standing.

Fortunately, what ‘quality’ means has been distilled into the WordPress coding standards. These will help you ensure that your code is up to par, and give you a roadmap of sorts to follow when working on important projects.

In this post, we’ll look at the WordPress coding standards in more detail. We’ll also introduce the GitHub version of the standards, and explain why you may want to use them as your go-to resource. However, before that, let’s talk a little about code quality!

A Quick (and Friendly) Lecture on Striving for High-Quality Code

We’re willing to bet that you’re someone who takes immense pride in what they do. At the very least, if you’re calling yourself a developer, it’s a smart idea to act like one. You may have the mystique-enhancing hoodie, a Newton’s cradle, and a myriad of other cliches at hand, but can you say that your coding quality is at a level matching your ambition?

The reality is that no matter where you’re at, there will always be improvements you can make. In a nutshell, your attempts to deliver the best work possible will usually take you through the following cycle:

  1. You identify (or have identified) some aspect of your code that’s not meeting your standards.
  2. Through a period of learning and implementation, you improve your ability.
  3. You identify another area of improvement you could make.

Imagine a bell curve, with your worst skills plotted to the left, and your best to the right. You should constantly be looking towards the left, and picking the shakiest elements of your coding to work on.

This will shift the whole bell curve to the right, and cause other aspects to become the new ‘worst’. This practice is called the ‘Inchworm’ technique and serves as a disciplined way of constantly evolving your abilities. The best news is that WordPress developers have an advantage when it comes to implementing this technique.

The Measures WordPress Takes to Shepherd You into Strong Coding Practices

Striving to develop your poorer areas is a noble goal, and a helping hand along the way will always be welcome. Naturally, WordPress’ core developers have a vested interest in making sure the community has a shallow learning curve when creating for the platform. If WordPress were difficult to code for, it wouldn’t be adopted by nearly as many developers.

For starters, consider how WordPress itself is coded. It’s practically sewn together by hooks, meaning that you can implement basic functionality across every project, without introducing any esoteric issues. You can also see this developer-friendly stance in how templates are used to piece together layouts, and even throughout the comprehensive comments contained within each PHP file.

In fact, WordPress has such a solid reputation in this area that regular users often crack open a code editor and tinker in their own sites’ back ends. There’s also the official Codex, which is full of reference material on how to achieve practically anything within WordPress.

As for developers, perhaps the only document you’ll need to ensure your code is high quality (and keep potential issues to a minimum) – is the official coding standards.

Introducing WordPress’ Official Coding Standards

The Core Contributor Handbook offers plenty to digest on the matter of WordPress core development. However, it also contains the official coding standards, which is what we’ll be focusing on here.

As you may have gathered, these standards contain the guidelines for committing code to WordPress’ core. Although we’ve talked about why they shouldn’t necessarily apply to theme and plugin development, they’ll still be helpful in those situations too.

In short, you’ll use the official standards to write code that validates and introduces no basic errors. Let’s quickly go over each set quickly:

  • PHP. This large collection of standards, relating to indentation, brace style, formatting SQL statements, and much more, is arguably the most important set for WordPress users.
  • HTML. Given the relative reduction in complexity, HTML has a few less entries than the other languages do. Even so, important elements such as indentation and self-closing tags are represented here.
  • CSS. Next to PHP, CSS is the most important language for styling. There’s plenty to look over in this set, although there’s more general instruction than specific elements.
  • JavaScript. JavaScript is understandably the most dense of all the coding standards here, due to its fledgling implementation in WordPress. In fact, you can consider this to be a mirror of the PHP coding standards, especially as it grows in prominence down the line.

Once you’ve spent a little time with them, you’ll notice that even the most comprehensive of these standards are still easily digestible. This is good news, as it means you’ll be able to implement them without wading through tons of instructions.

Finding an Up-to-Date Version of the Coding Standards (and Making Sure You Stick to Them)

Before we finish up, we’d like to make you aware of GitHub – specifically the WordPress Coding Standards page. This is similar to Make WordPress’ standards, but there are a few additions representing more current changes, which aren’t outlined on the official site.

In our opinion, this makes the GitHub version the de facto official coding standards. Version 1.0 was released in July 2018, and has one key difference – it’s essentially a collection of PHP_CodeSniffer rules (or ‘sniffs’). If you’re unfamiliar with this tool, it tokenizes your PHP, CSS, and JavaScript, and detects any differences or violations between your code and the standards.

If you’re following the standards for core contribution (as you should be), leveraging these sniffs is practically essential. We recommend reading through the GitHub documentation and picking your preferred installation method. Then you can leverage some of the additional sniffs available, such as those for inline documentation, and you’ll have access to some best practices not included within the core standards.

Conclusion

We can use an old programmer’s paradigm to describe how you should approach every aspect of your coding: “Garbage In, Garbage Out.” Ultimately, if the quality of your code is any less than top-notch, you’re doing yourself and your users a disservice.

Fortunately, WordPress has a documented set of standards any developer can follow to ensure that their code is worthy of inclusion. By following these standards, you all but guarantee that the dedicated review teams will approve your commits. This gives you a head start on beginning your next project.

Do you have any questions about how to use the WordPress coding standards? Let us know in the comments section below!

Tom Rankin is a key member of WordCandy, a musician, photographer, vegan, beard owner, and (very) amateur coder. When he’s not doing any of these things, he’s likely sleeping.

The post The WordPress Coding Standards: An Introduction appeared first on Torque.

Sharing is Awesome, Thank You! :)

Share this Blue 37 post with your friends
close-link