Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 PHP Coding Standards

In this lesson, you'll learn what the WordPress PHP coding standards are and why they're important when programming PHP for WordPress.

Related Links

2.2 PHP Coding Standards

Hello, and welcome back to this Tuts+ course on writing PHP for WordPress. In this part of the course, I'm gonna take a look at the WordPress coding standards. Identify why they are so important and walk you through through some of the ones that you're most likely to need to make use of. Here are the PHP coding standards, and they are just part of the overall WordPress coding standards. So as you can see here, there are standards not only for PHP but also for accessibility and for the other languages that you'd use within WordPress. 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 gonna 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. So 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. So let's take a look at some of the PHP coding standards. And I'm gonna show you examples of these in some of the files in my theme. So I'm gonna start with naming conventions and cases. Now let's take a look at this file, the Functions file in my theme. So when you're thinking about naming conventions, there are four things to think about. There are the functions within your theme or your plugin. There are the files and the way that you name those. There are any classes that you write. And there are also any variables that you create. So let's start with functions. So you can see here I've got a function that I've defined called rachelmcc_register_widgets. Now that's all written in lowercase 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. Now that is 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 had a function called register wigits, the two of them could clash. And 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. Cuz my theme is called rachelmccollen so my functions have that as a prefix. And it's also important to use underscores for your functions and not to use hyphens. Now where you do use hyphens are two places. Firstly, when you're defining CSS classes. So you can see here 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. And the other place that you use hyphens, and you can actually see it up here, is in your filenames. So this is front-page.php. And you should always use hyphens in your filenames within your themes and your plugins, don't use underscores. So here there is a call here using locate_template. And I can see actually that's incorrect. I've used spacing wrong there [LAUGH]. See, we all get it wrong occasionally. loop-frontpage.php is a file, a template part that I'm calling there. And you can see that that's got a hyphen and not an underscore. Whereas my post_type ID, that's a custom post_type I've registered. For that I'm using an underscore. Now you also might need to think about variables within your theme. 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 here I'm using lower case for both of those. Now the other place where you need to think about your underscore is in your capitalization when you're using a class. And you can see here, I'm using the WP_ Query class. So a class has a capital letter here after the underscore, as well as at the beginning. So that's where you use capitalization when you're defining a class. And that helps people working with your code instantly identify that that's a class and not a function. So that's some of the naming conventions that you use in PHP for WordPress. Now let's have a look at how you would use single and double quotes within WordPress. So you can see here I've got some double quotes here because this is actually HTML here. And 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 it makes it easier to work with special characters. Now, I've got an example here. So say if 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? Now I've put that within double quotes, and the reason I've done that is because we have these apostrophes here, there are three of them. So let me show you how that would work if we did it in single quotes. So I'll change those double quotes to single quotes. And you can see immediately within my code editor it's showing me that there's errors. And that's because I need to put a backslash before those special characters in order for them to be output correctly within the HTML. 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. So you can use either. 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. 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 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. So 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. So that's how you use quotes within PHP for WordPress. Next thing, I'll look at indentation. And this file is quite a good example of it because I've indented a number of lines of code within it. So you can see here 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, you can see here I've got an array that's all within that, everything inside that should be indented. And also for indentation, you should use line breaks. So I'll correct that in this file here and I can work my way down and correct the rest of that file in a moment. So let's take a look at another file where there's single lines as well as double lines. So here you can see I've got a if statement that's got two lines of code within it. So I've put a line break, sorry, above and below those two lines of code. If there was only one line of code within that, so if I delete that, I wouldn't have to put those line spaces in, I would just put that straight inside my braces. Let me just undo the change I've made there, cuz that won't work that far without it. Using spacing like this helps make it clear where your blocks of code are. So this is a block of code that is run if this condition is met. And you can see also that indentation has been used. So there's multiple layers of indentation here. 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 again depends on whether you're coding a block of code or a single line. So where you've got a block of code, you put your opening tag on one line and your closing tag on another line. And that helps to make it obvious that this is 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. Now if I was to write a line with PHP that was just one line, I would put the PHP opening and closing tags on the same line. So you can see here we've got PHP if half posts, this is the opening of the loop, and this is 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. So that's an introduction to some of the elements of the 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. Now if you want to know more about the coding standards, there's documentation on the WordPress website. And there's also this series here which is on the Tuts+ website, which goes into all of the elements of the coding standards in detail. In the next part of this course, we're gonna start creating some PHP. So I'm gonna walk you through the process of creating a PHP file in your theme and adding some PHP to it. See you next time, and thanks for watching.

Back to the top