Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:15Length:1.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Adding Include Files

In this lesson, you'll learn how to add include files for extra PHP code, and when it's a good idea to do so.

Related Links

3.3 Adding Include Files

Hello, and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, I'm gonna show you how to include files in your plugin. Now, this guide here from Tuts+ does relate to templates. But there's a lot in it about the core functions that are relevant to plugins. So here we've got include, include_once, require, and require_once. And they work in slightly different ways, as you'll see if you read this guide. Now, the code in this guide here uses functions like get_template_part. You don't want to do that, and I'll show you how to refer to your plugin files within your plugin. So let's go back to the code, so here's my plugin. I'm gonna update the version number, and I'm going to add my include files. So I'm gonna copy this commented-out text here, save typing all those asterisks again. So let's start by including a file. Now, the difference between including and requiring files and other functions that you'll have seen on this course is that I'm typing my include function straight into my plugin. I don't need to put it in a function and then attach that to a hook. Include just works exactly where you are in the plugin, so the first parameter is the path for my plugin. And there's that FILE again, so after that, I need to add, The name, Of my file, and that needs to go in inverted commas. We have here one parameter, and that's the path to my file. So include this plugin directory path relating to this file, so that's the plugin where this file is. And then I want includes, and then register_post_type.php. Now, that doesn't exist at the moment, so let's create it in the plugin. So we create a new folder called includes, and within, sorry, within that, We create a new file called register, post_type.php, now, that's a good example of where you might have some quite discrete function within your plugin. That isn't the core of your plugin, but it's important to it, and that you add that to a separate include file. So, for example, if my plugin is one to set up a store or to set up a photography gallery, but I need to register a post type for that to work. Then that include file could be kept separately so I can keep that code on its own and find it easily in the future. So at the moment, I'm including that file. And if that file can't be found, the plugin will just continue on its merry way and ignore what's happened. But what if I wanted to ensure that that file is definitely there, and if it's not there, the plugin throws an error? Well, I simply change that to require. So if I use require instead of include, it's required, it has to be there. And if WordPress finds at this point that this file doesn't exist, it will stop and it will throw an error. If I use include and it doesn't find it, it will just skip over it and carry on with the rest. So if, for example, you're running a store and your post type is products, it's not gonna work without your products custom post type. So it's useful to have require. And then there are two other versions of these functions, and they are include_once, And require_once, Which do what you might expect, they only include them once. So if that file has already been included elsewhere in your plugin or required, it won't do it again, it'll only do it the once. But if you want to insert that code all over again, you can just do it using include as many times as you want. So you might do that if you've got a snippet of code that you've put in an include file that you can use in multiple places. But something like this, where you're registering a post type, you only need to do that once. So you might use require_once for that because you need to ensure that it happens, and you only want it to happen once. So that's how you include files within your plugins. And it's a good way of managing large plugins with complex code that can be easily split down into different modules. In the next part of the course, we're going to start looking at some specific uses for plugins. And we'll start by looking at plugins that customize the admin screens. See you next time, and thanks for watching.

Back to the top