What are the WordPress PHP coding standards? In this video from my course, Learn PHP for WordPress, you'll learn all about the coding standards and why they're important.
The WordPress PHP Coding Standards
What Are the WordPress PHP Coding Standards?
You can find the full WordPress PHP coding standards in the official WordPress handbook.
They're essentially a set of best practices on how to use PHP in WordPress. We'll go through some examples of what that means in practice in the rest of this tutorial.
As you can see, there are standards not only for PHP but also for accessibility and for the other languages that you'd use within WordPress.
Why Are the PHP Coding Standards Important?
Why is it important that we all adhere to these standards?
Well, there are two reasons. One of them is about quality of code, and the other is about consistency.
So firstly, quality is important because it means that everybody coding in WordPress is creating code that will work, that will do its job, and that will be written in an up-to-date and high-quality fashion.
The second, which is about consistency, is equally important. It's very likely that you're going to be working with other people's code from time to time. For example, if you're creating a child theme, you might have to copy some of the code from the parent theme. And if you're creating a plugin, that plugin could be a fork of an existing third-party plugin that you're copying and adding extra functionality to. It's really important that the way you code is consistent with the way that everybody else who codes PHP for WordPress writes code themselves.
Examples of PHP Coding Standards for WordPress
So let's take a look at some of the PHP coding standards. And I'm going to show you examples of these in some of the files in my theme.
Naming Conventions and Cases
Let's start with naming conventions and cases. When you're thinking about naming conventions, there are four things to think about:
- the functions within your theme or your plugin
- the files and the way that you name those
- any classes that you write
- any variables that you create
So let's start with functions. Here's the functions.php file in my theme as an example.
You can see that I've got a function that I've defined called
rachelmcc_register_widgets. That's all written in lower case, and I'm using underscores between the words, which is how you should always write a function in WordPress.
As you'll also see, I've used a prefix. That's to ensure that this function doesn't clash with any other functions that might be provided by my theme or by another plugin. So if I wrote a function that was just called
register_widgets and one of my plugins also had a function called
register_widgets, the two of them could clash. You might find that one of them overrides the other or that the plugin doesn't work.
So instead, I'm using a unique function that's relevant to my theme. My theme is called
rachelmccollin, so my functions have
rachelmcc as a prefix. And it's also important to use underscores for your functions and not to use hyphens.
You do use hyphens in two places. Firstly, you use hyphens when you're defining CSS classes. So you can see that within my widgets, I'm defining CSS classes for my
h3 element and also for the
id of that widget area. And here, you use hyphens.
The other place that you use hyphens is in your file names. So this is front-page.php.
You should always use hyphens in your file names within your themes and your plugins; don't use underscores. So here there is a call using
loop-frontpage.php is a file, a template part that I'm calling, and you can see that that's got a hyphen and not an underscore.
On the other hand, my post type ID, which is a custom post type I've registered, uses an underscore:
Now you also might need to think about variables within your theme. Let's look at loop-frontpage.php:
Within this particular file, which is the one I was calling from that previous file, I've defined some variables, one called
count and one called
title. And you can see that I'm using lower case for both of those.
The other place where you need to think about underscores and capitalization is when you're using a class. Let's go back to front-page.php:
And you can see here, I'm using the
WP_Query class. So a class has a capital letter after the underscore, as well as at the beginning. So that's where you use capitalization when you're defining a class. It helps people working with your code instantly identify that it's a class and not a function.
Using Single and Double Quotes
Now let's have a look at how you would use single and double quotes in PHP for WordPress.
You can use either single quotes or double quotes, depending on which works best for the specific code that you're working with.
The reason that double quotes can sometimes be more helpful is that they make it easier to work with special characters. Here's an example:
Say you were writing a search template file, search.php. And within that, if nothing was found, you would put a paragraph that says, "I'm sorry, your search didn't find anything. Why don't you try again?"
I've put that within double quotes because the text contains apostrophes. Let me show you how that would work if we did it in single quotes.
You need to put a backslash before the apostrophes in order for them to be output correctly within the HTML—otherwise, it'll give you errors. Now I don't know about you, but I'd rather just type it normally within double quotes than do all this within single quotes.
The other thing you need to remember is that if you need to put quotes within quotes, you either put single quotes within double quotes, or vice versa. It doesn't matter which way around, but you can't put single quotes within single quotes or double quotes within double quotes.
So here's an example going back to my functions.php file.
Within here I've got single quotes for the value of
before_widget, and then within that, I've got double quotes for the ID and the class of that. I couldn't use double quotes here and then put double quotes inside it, so that's why I'm using single quotes there, because it's better to use double quotes with your CSS.
Now, let's look at indentation. And let's continue looking at the functions.php file, which is quite a good example because I've indented a number of lines of code within it.
You can see that the function name is on the left, and then within that the
register_sidebar function is indented at one level. And the contents of that are indented again.
So each time you're putting content within braces or curly braces, everything inside that should be indented. And also for indentation, you should use line breaks.
So let's take a look at another file where there are single lines as well as double lines.
So here you can see I've got an
if statement that's got two lines of code within it. So I've put a line break above and below those two lines of code.
If there was only one line of code within that, I wouldn't have to put those line spaces in—I would just put that straight inside my braces.
Using spacing like this helps make it clear where your blocks of code are. And you can see also that indentation has been used. So there are multiple layers of indentation.
The other thing you need to bear in mind is where you open and close your PHP tag and whether that is on the same line as your code or a different line. And that also depends on whether you're coding a block of code or a single line.
Where you've got a block of code, you put your opening tag on one line and your closing tag on another line. That helps to make it obvious that it's a block of PHP and that everything within those tags is PHP.
The other thing you have to make sure you do is use a full opening PHP tag. So don't just use a question mark, as you might have done in some other systems that use PHP, because in WordPress you have to use the full PHP tag.
If I wrote some PHP that was just on one line, I would put the PHP opening and closing tags on the same line.
You can see that we've got some PHP that's all in one line, and then after that I've got HTML. So the opening and closing tags go on the same line as my code.
That's an introduction to some of the elements of the WordPress PHP coding standards, and I've shown you some examples of them in my files and changed some as I've gone along to show you how things are done correctly.
If you want to know more about the coding standards, there's documentation on the WordPress website. And you can also check out this Envato Tuts+ series, which goes into all of the elements of the coding standards in detail.
Watch the Full Course
In the full course, Learn PHP for WordPress, you'll learn all about PHP, the programming language that WordPress is built in.
I'll give you an overview of what PHP is and how it's used for WordPress themes and plugins, with examples. You'll go on to learn how to create a PHP file and use it to output HTML. Then you'll learn to use functions, loops and if statements for coding custom WordPress themes and plugins.
You can take this course straight away with a subscription to Envato Elements. For a single low monthly fee, you get access not only to this course, but also to our growing library of over 1,000 video courses and industry-leading eBooks on Envato Tuts+.
Plus you now get unlimited downloads from the huge Envato Elements library of 580,000+ creative assets. Create with unique fonts, photos, graphics and templates, and deliver better projects faster.
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post