Advertisement
Cheat Sheets

The WordPress Coding Standards: An Introduction

by

When it comes to building WordPress-based products, we're somewhat cursed (or blessed, depending on how you see it), with a double-edged sword: Because WordPress is written in PHP, it's relatively easy to get WordPress - or the project itself - to do whatever it is we want to do all the while avoiding best practices.

But this raises the question: What's the point of an API or formal coding standards if we're simply going to ignore them?

We've written quite a bit about the WordPress APIs in previous articles, and we've touched on the WordPress Coding Standards, but we've never really taken a deep dive into the coding standards, understanding each aspect of them, and why they matter.

So in this series, we're going to be doing just that.


Why Bother With Coding Standards?

Before taking a look at everything that we're going to be covering, I think it's important to understand why coding standards even matter.

WordPress is widely used by a lot of people using a lot of sites. There's a lot of code being employed out there that's all built on top of the core application. This means that there are developers and designers who are working to maintain those code bases.

The challenge comes when the person who didn't originally write the code is stuck with improving, managing, and fixing bugs for the project. Let's say that the first person didn't follow the coding standards, but the person who is now maintaining the code is used to following best practices.

They now have to deal with code that looks nothing like what WordPress code should look like.

And that's really the crux of the matter: The code that makes up our themes, our plugins, and in our applications should ideally look like a single developer wrote the code.

That's what coding standards enforce.

Unfortunately, standards are either ignored or they aren't evangelized enough despite the fact they are well-documented. That's where this series of articles comes into play: We're going to be looking at each of the standards in-depth to evangelize, to understand, and to encourage one another to begin doing a better job.


A Look at the Coding Standards

This series is going to include seven article in addition to this introductory piece.

Here's where we are headed with this particular series:

  1. Naming Conventions and Function Arguments - We'll take a look at how we should name our variables, methods, and classes such that they are clear, easy to understand, and follow for ourselves and other developers.
  2. The Use of Single Quotes and Double Quotes - Single quotes and double-quotes result in different behavior in PHP and there is a time and a place for when to use which in WordPress. In this article, we'll take a look at examples of each and the rationale behind what we're doing.
  3. Indentation, Space Usage, and Trailing Spaces - White space places a big part in writing WordPress-based code. This article is going to explore the reasons why indentation and space matter as well as when to use tabs and when to use spaces.
  4. Brace Style, Regular Expressions, and PHP Tags - Different C-style languages use different syntax. This article will examine the proper way to place braces in your WordPress-based code. We're also going to examine the proper way to include regular expressions, as well as some of the gotchas and best practices as it relates to using PHP tags.
  5. The Ternary Operator and Yoda Conditions - Conditional statements are a corner stone of programming and they are used liberally throughout WordPress. PHP offers a shortcut for if/else statements, and WordPress opts to evaluate conditionals differently than what other languages often support. Here, we'll take a look at all of the above.
  6. Database Queries and Formatting SQL Queries - There's a full API for running queries against the WordPress database, but there are also ways to run raw SQL. If you ever need to go that route, there's a right way to do it. This article will take a look at exactly that.
  7. Bringing It All Together - Finally, we're going to summarize everything that we've discussed throughout the series to make sure that we're all on the same page, and to urge us to move forward with doing a better job of adhering to the standards.

Conclusion

When it comes to blogging, it's often assumed that the author is an expert in the topic at hand, but the truth is although I firmly believe that we should follow the coding standards and that I try to adhere to them to the best of my ability, I'm learning things each week.

I've violated the coding standards plenty of times, and I'm sure some of you have, as well.

The point of this series is to walk through the standards together so that we may all come out as better developers once we're done.

Related Posts
  • Code
    Creative Coding
    Using WordPress for Web Application Development: A ReviewApplication foundation 400
    Over the past few months, we've been taking a look at all of the features and aspects that make WordPress a potential foundation for application development. In fact, we've spent roughly 15 articles talking about all that WordPress offers. And though we'll be reviewing each of the points in this email, perhaps the biggest thing to take away that building web applications using WordPress is different than using many of the popular frameworks that are currently available namely because WordPress isn't a framework.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: Custom Database QueriesApplication foundation 400
    Throughout this series, we've been looking at the various facilities that make it possible to treat WordPress as a foundation for web application development. Thus far, we've covered a lot of ground: We've talked about how WordPress is more of a foundation rather than a framework. We've discussed the nature of the the Event-Driven Design Pattern. There's been a discussion of Email, User Management, Saving Data, Retrieving Data ...and more. In the most recent articles, we've been talking a look at how to handle queries against the WordPress database through the use of WP_Query and WP_User_Query.Read More…
  • Code
    Theme Development
    Making the Perfect WordPress Theme: How to Code WellWp white 400
    In the previous part of this series; we went through various WordPress APIs that we should learn about, talked about the importance of making a theme translatable (or even better, releasing them already translated into other languages) and understood the concept of licensing themes and using licensed products with the themes. In this article, we're going to focus on code: We'll see how to code with WordPress coding standards, how to properly comment our code and how to validate and test the theme.Read More…
  • Code
    Cheat Sheets
    The WordPress Coding Standards: Bringing It All TogetherThe wordpress coding standards
    When it comes to writing a series of blog posts, one of the most challenging aspects as a reader is actually keeping up with every post that is published. Even if you do manage to try and keep up, posts that are in excess of 1,000 words - especially those that include code - can take time that many of us don't have especially when it comes to juggling our work lives, home lives, hobbies, and other things. So in order to make sure that the information presented throughout this series is still presented in a digestible way, I thought I'd experiment with doing a summary of the entire series. That way, for those of you who have missed an article or haven't had the time to sit down and go through each article, can still get the gist of each point mentioned throughout the articles. With that said, let's take a look at everything we covered when reviewing the WordPress Coding Standards.Read More…
  • Code
    Cheat Sheets
    The WordPress Coding Standards: The Ternary Operator and Yoda ConditionsThe wordpress coding standards
    At this point in the series, we've covered a lot of ground. Up to now, we've discussed the following topics: Naming Conventions and Function Arguments The Use of Single Quotes and Double Quotes Indentation, Space Usage, and Trailing Spaces Brace Style, Regular Expressions, and PHP Tags Lots of stuff, right? In this particular article, I thought we'd take it a bit easier before jumping into the final topic. As such, we're going to cover two really simple topics (that are often either ignored or overcomplicated). Specifically, we're going to talk about the ternary operator and we're going to talk about Yoda conditions.Read More…
  • Code
    Cheat Sheets
    The WordPress Coding Standards: Braces, Regular Expressions, and PHP TagsThe wordpress coding standards
    In this series, we've been taking a deep dive into the WordPress Coding Standards in order to get the word out about them, understand them, and begin to practically apply them in our day-to-day work. If you're just joining the series, so far we've covered the following topics: Naming Conventions and Function Arguments Single Quotes and Double Quotes Indentation, Space Usage, and Trailing Spaces In this article, we're going to continue building on top of the content in the previous article: Specifically, we're going to be taking a look at brace style, regular expressions, and nuances of working with PHP tags within the context of building WordPress themes, plugins, and applications.Read More…