Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.1 WordPress PHP Functions

In this lesson, you'll learn how to write PHP functions for your theme or plugin. I'll show you how to code a PHP function and add it to the theme or plugin's functions.php file.

Related Links

4.1 WordPress PHP Functions

Hello, and welcome back to this Tuts+ course on learning PHP for WordPress. In this part of the course, I'm going to teach you how to write a function. A function is a really important piece of code that you can add to your themes or your plug-ins. And the reason you would use it instead of coding directly into your theme or plug-in file is that it lets you use the same code in more than one place. It also lets you keep all of your functions or your functionality in one file in your theme. Instead of having it scattered around your theme template files. So let's take a look at the files in which you might find some functions. So the first of these is your theme functions file, which is the functions.php file in your theme. And you can use this for adding any functionality to your theme. So here you can see I've got quite a lot of functions, I've got one for fonts. One for a burger menu, one for widgets, post types, and that one within it has got a WordPress function. And what you'll find is that WordPress has got a whole bunch of built-in functions that you can use either directly in your template files or within a function. So here in the code reference for WordPress, you'll see a whole list of all the functions that are already coded into WordPress that you can make use of. So functions are already built into WordPress. And as I'll show you, when you write your own, you need to make sure you don't duplicate the names of any plugins that are already in WordPress. Or might already be inside any themes or plugins that you're using, and you do that by using your own prefix, which I'll show you. So going back to our functions file, you can see some of these functions are actually quite long. So this function here, registering post type, spans multiple lines. And that's because I've got quite a lot of custom post types in my site. Whereas this function here, for registering and then queuing a style, is only a couple of lines long. So as you can see, you can include as much code as you want within a function. Now functions used within your functions file to add functionality to your theme that you can't have directly in theme template files. Such as registering a widget, or a custom post type. But you can also use them for code that you want to be able to access from multiple places within your site. So, for example, this function that I've got here, which is to display a featured image within a single post. Now, in this case, I've got a conditional function within it that checks that we're in a singular post. And also checks if there is a post thumbnail, in other words, a featured image. And I've hooked that function to the compass postmeta before action, which is one I've coded into my theme. If I wanted to use this more than once, I could hook it into multiple places in my sight. And I will show you how to hook functions in the next part of the course. But it's worth bearing in mind that a function is a snippet of code that you can then use anywhere you want in your frame. Or in your plugin simply by calling it. So let's have a look at how you write a function, the first thing you do is you type the word function. And that tells WordPress that this is a function. You then type the name of the function, and this needs to be a unique name. Now, the way you ensure that your name is unique is by using a prefix. So I might use rachelmcc, for Rachel McCollin, and then I put something that's relevant to this function. So my function is called rachelmcc say hello, and that's because I'm going to echo out, hello world. Now the next thing you do is you have to put brackets after your function name. And you'll have noticed when I did that, in my code editor, the color changed, and that shows that it's correctly coded. You then enclose your function in curly braces. And in my code editor, when I added an opening brace, it automatically added a closing one for me. Now what I tend to do is write my functions on multiple lines and put lots of spaces in, so I can see what I'm doing. So I've got spaces around the code within my function, so now I'm going to write the code that is inside my function. Now you'll see in my code editor, there's lots of red at the moment, and that's because I haven't properly closed my line of code. And when I do, it all disappears, and the only red is the text within that echo function. So that's a really basic function. Now you can add anything you want to your functions, and that will depend on exactly what it is you want to do in your theme or plugin. Now if you want to learn more about writing a function in detail. This coding basics course will show you exactly how to write a detailed function for WordPress. So let's go back to our code file. Now I'm going to show you how to write something that's called a pluggable function. And I'll show you an example of this in my widgets file within my framework theme. Now this theme is designed to be used as a parent theme. And I always create a child theme for any new signs I build that runs off this parent theme. So, in this parent theme, there are lots and lots of functions, and sometimes I want to override those functions in my child theme. Now, the easiest way to do that is by making the function in my parent theme pluggable. And what that means, is it'll only run if there isn't another function with the same name somewhere present. And that somewhere present is in the child theme. Because the child theme's functions file is run before the parent themes. Now if you don't make your functions pluggable, and you write two functions with the same name. It'll throw an error, and you'll get the white screen of doom. So it's a good idea to make as many functions as possible pluggable. Especially in any theme that could be used as a parent theme, or in a plugin as well. Because somebody might create another plugin to override it. So, you can see here what I'm doing is checking that a function does not exist using that exclamation point. So, let's go back here. So I've got an if statement, and then I've got an exclamation point because I'm checking that the function doesn't exist. And in here, I put the name of my function, so that's going to be rachelmcc say hello. So now it'll check whether that function already exists. Now, you shouldn't add two functions like this in the same file, what you'll find is that they're in different files. But this just shows you how it works, and then I put my function. Now you can see here I've got an error here because of my apostrophe. If I put a backslash before that apostrophe, and I have to admit finding the backslash on the keyboard can be a struggle. There we go, I. There we are, that will get rid of that error. So that will just echo out, I'm not in a good mood today, I'm not going to say hello. But this pluggable function isn't quite complete yet. Because what the conditional check is doing is checking if there's an existing function before we actually create our new function. So what I have to do outside my echo is add a new function, and that has to have the same name as this. So, here we have a conditional check to see if the function already exists. If it's already been called with another function with the same name. If not, we're creating a function called rachelmcc say hello, and that's how you create a pluggable function. So if I then create another function with the same name in my child theme. Or later on in this same theme, that will override this pluggable function here. So that's two types of functions that we've created, a basic function and pluggable function. In the next part of the course, I'm going to show you how to run your functions. And there's two ways to do that, by adding them directly into your code, or by attaching them to an action or filter hook. See you next time, and thanks for watching.

Back to the top
View on GitHub