Part of the reason for the success of WordPress is how easy it is to customize with themes and plugins. There are a huge number of plugins available in the WordPress Plugin Directory or on Envato Market, but if you want to take your WordPress skills to the next level, there’s no substitute for learning to code custom plugins.
In this course, you’ll learn about how plugins work and how you can create different kinds of plugin. This is a comprehensive guide to everything you need to know to write your own plugins! You’ll start with the basics—how to create a new plugin, how to structure your code, and how to connect to the plugin’s API. Next, you’ll learn some of the fundamentals of plugin development, like including scripts and styles. Finally, you’ll learn the building blocks of creating WordPress plugins: custom content types and taxonomies, adding settings to the admin page, outputting HTML content, and creating widgets and shortcodes.
Do you want to learn WordPress development from start to finish? Check out our learning guide: Learn WordPress Development.
Welcome to Introduction to WordPress Plugin Development. In this course, you’ll learn all the key points of WordPress plugin development—everything from creating your first plugin project to registering widgets and shortcodes. After following along with this course, you’ll be all set to develop your own custom plugins for WordPress!
Hello, and welcome to this Tuts+ course on WordPress plugin development. In this course, you’ll get a good grounding in all the different aspects of plugin development and what you need to know to write your own plugins. So we’ll start by identifying when you should write a plug in instead of using the functions file. We’ll then look at the structure of a plugin, and I’ll use a big complex plugin like Woo Commerce to demonstrate how you should organize your own plugins. We’ll then move on to some practical examples of plugin code. So I’ll show you how to set up a plugin and how to do things like in queueing style sheets and adding include files, and then I’ll show you some specific examples on how to write plugins to create things like widgets and short codes, and to query the database. We’ll then pull together a bunch of the techniques we’ve used in the course to create a custom widget, which will register a post type, output a query, and add all those into a widget that you see here on the site. Watch this course and get a thorough grounding in how to write your own plugin.
In this lesson, I’ll give you an overview of how the course is structured to help you follow along and gain a comprehensive introduction to plugin development.
Hello, and welcome back to this Tuts+ course on WordPress plugin development. This course works slightly differently from many of the other courses you might have seen on the Envato Tuts+ website. So in this part of the course, I’m gonna show you how the course will work and give you an idea of what to expect. Because I want to give you a broad grounding in all the aspects of plugin development, I’m not going to develop just one plugin during this course. Instead, I’ll refer to a number of plugins that are available via Tuts+ and that you’ll see in other courses. So the code will apply to various parts of the course. And you can see that this is a long course with lots of code. So for example, in the next part, Part 3.1, I’m gonna show you the code from a plugin to register taxonomies. And I’m also going to show you a theme. Because, that way, I’ll demonstrate the difference between using your functions file and using a plugin. And as we work through the course, you’ll see examples of code and plugins. So for example, we’ll look at WooCommerce, which is a big third-party theme, and I’ll use that to demonstrate how to structure your own plugins. In parts 4.1 to 4.4, we’ll get a little bit more hands-on. And you’ll start coding your own sample plugin if you work along with me. So what we’ll end up with is a sample plugin that has some structure, with some includes, some scripts, and some styles, and has this core file. In which, I’ll show you how to enqueue scripts and styles and how to include your files. And I’ll also show you how to set up your plugin and add this commented out text. So that you know exactly how to start writing your first plugin. We’ll then move on to some more examples where we’ll be looking at existing courses and the code for them. So for example here, we’ll look at this section-menus plugin. And I’ll use that to demonstrate how you output content using the results of a query. So instead of creating one long plugin as we go along, we’ll be creating one small plugin to show you the basics of plugin development. But I’ll also use lots of existing plugins. And I’ll work through the code of those in detail with you to demonstrate how they do what they do, and how you can achieve different functionality with your plugins. And as we go along, I’ll show you the courses that they relate to, and I’ll give you a link to them as well. I’ll also refer you to a demo site that I’ll be using in some parts of the course. And we’ll look at the WordPress codex in various places. And I’ll give you links to the relevant codex places for the different aspects of the course. So when you finish this course, instead of understanding in depth how to code one type of plugin, you’ll have a broad grounding in how plugins work. And how you can use plugins to achieve different things in WordPress. And you can then take that knowledge to go on and learn how to code specific plugins in WordPress using our courses. In the next part of the course, I’ll start looking at how you would use a plugin. And I’ll compare the use of a functions file in the theme to coding a plugin. See you next time and thanks for watching.
First, let’s look at when you should write a plugin and when it would be best to just use your theme functions file.
Related Links
Hello, and welcome back to this Tuts+ course on plugin development. In this part of the course, I’m gonna give you an overview of when you should use a plugin as against coding something into the functions file in your theme. Because it’s very tempting just to use that functions file for everything, and to stick loads of code in there that really should be going into a plugin. So let’s take a look at the difference between the two. So here’s an article that will help you identify which is better. But I’m gonna show you with reference to two plugins and themes that I’ve already developed for Tuts+. For other courses, so we’ll start with this course which is a guide to the loop in WordPress. And that uses a theme. There’s no plugin involved in this particular course. And it gives you a good opportunity to see what What sort of functionality you would add in the functions file in a theme. So this is the theme for that particular course. And you can see it’s got a number of template files. And it’s also got some include files such as the footer, and the sidebar, and so forth. And these include files to including the loop because it’s a guide in the loop. But what we’re interested in is the functions file. And this includes two functions which are relevant to themes and that you wont add to a plugin. So the first one is to set up the theme. So every well coded theme should have a theme set up function, which is hooked to the after set up theme hook And this includes setting up the title tag, the text name, post format, post thumbnails, RSS feeds, and also the navigation menu. So those are things that are specific to the theme, they’re about the way that your site displays. So the functions file and any code that you to the theme Is about display, it’s about the way your theme makes your site look, whereas a plugin is about functionality, and code that you put into a plugin will be code that you could use regardless of what theme you’re running. So it’s code that you want to hang onto if you switch themes in the future. And here’s another example of a function that is relevant for using in the theme functions file. So that’s a widgets function. And obviously, the widgets are specific to the theme. You wouldn’t register widgets via plugin. And bear in mind these are widget areas, not widgets. So I’ll be showing you how to create a plugin for widget that you can put in those widget areas. If I’m not confusing you with these terms later on in the course. But here in the functions file is where you register the widget areas, the side bars in your theme. So in this one we’ve got a side bar and we’ve got three footer widget areas, and those have to be registered in the theme. So those are good examples of functions you would add to your theme functions style. But before you go adding any and all functions that you want to add to your site to your theme functions file. It’s important to consider whether it should be added to a plugin instead. And the real test is, do I want to keep this code if I switch themes in the future? And a good example of that is if you register a custom post type or a custom taxonomy. Because if you were to change your theme in the future, you wouldn’t want to lose access to any posts you’ve added of that custom post type. Or that you’ve used that custom taxonomy with, because they’ll still be in your database. So let’s have a look at another example. So here we have the course, how to use custom post types in WordPress. Now this course includes a plugin, to register the custom post type, and it also includes a theme. So if we go back into the code, here’s the theme. So I’m using this theme. To display my custom post types, so you can see for example here we’ve got archive.plusproject.php. But firstly, let’s take a look at the plugin. So this plugin registers a post type called Project, and it includes The standard way of registering a post type in WordPress. Now a lot of people add this to their theme, because they think that it’s linked to those archive pages and template files that are relevant for that post type. But in reality, it’s much better to register a post type using a plugin, because that way if you change your theme in the future, you decide to use a third party theme Or you code a new theme for yourself, it means that you still got your post type. And you’ve still got access to all of the data in the database using that post type. Now that data will stay in your database, even if the post type isn’t registered. So if you were to remove this plugin deactivate it, the database tables wouldn’t change and you’d still have those posts in your database. So as soon as you reactivate the plugin You find that your post types and all those posts that you created using that post type are straight back available in your site. So it’s important that you don’t lose that if you change your theme in the future. So, the functions file for this particular theme, and if I just take you back. That’s this Tuts+ custom post types theme, that I developed for that same course that functions file includes, including a style sheet Because it’s a chart of a parent theme, and that’s why it hasn’t got this theme set up up here, because that’s already included in the parent theme. And it also includes this function here, which displays projects on the homepage. And that’s add it to the theme here because it’s about the way the content is displayed. But arguably you could add this via a plug-in. You could create a completely separate little plug-in all of it’s own That changes the way that projects are added on your homepage. Or alternatively, you could add it to this particular plugin. You could add an include file with that code in it, and I’ll show you how to use include files later on in this course. So here’s the plugin I created for that course. And that registers the post type. And that post type is then displayed in these two template files within the theme. So we’ve got all kind of types for this project. And what I’ve done here is copy the archive template file from the parent theme. And use it to create a custom template file for my custom post type. And there’s also single post project which in the same way I copy the single template file from the parent theme and add it to my child Here, and that will display my post type that I’ve registered using that plugin. Now you might be thinking, if I was then to switch themes and I’ve still got my plugin registered, it gives me a problem because I haven’t got this archive page. I haven’t got this template file WordPress will always default, to another template file. It’ll work its way through the template hierarchy, and it will display your project. Using the most appropriate template file it can come across, using that hierarchy. So it doesn’t mean that you won’t be able to display your projects anymore. It just means that they’ll be displayed, using the template files from your new film. So that’s the difference between the functions file and a plug-in. Use the functions file to add code that is about the way that your theme works. So for example Navigation menus and so forth. And there’s also about the way that things are displayed in your theme. It’s about design, and the look of your site. Use a plugin for any extra functionality. And in particular, ask yourself the question, would I need this functionality if I change things in the future. And if that is the case You would code a plugin. In the next part of the course. I’m going to show you the structure of a complex plugin. And you wouldn’t be writing plugins this complex as you get started, but over time you might learn to and it gives you a good idea of how a plugin Is structured and how in particular, a large complex plugin is structured to ensure that the code all makes sense and is easily manageable and editable. See you next time and thanks for watching.
In this lesson, you’ll learn about the structure of plugin files and their code.
Related Links
Hello, and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, I’m going to use the WooCommerce plugin to demonstrate how to structure and organize your plugin. Now, WooCommerce is a huge and vastly complex plugin, but it’s incredibly well organized. I’ve customized WooCommerce many times on client sites and my own sites. And I’ve always found that with reference to the code reference provided by WooCommerce. And also by delving into the files and following a trail of code through the different files, I’ve always been able to find what I’m looking for. Because the plugin is very, very well coded and very well organized. So if you want to get a copy of WooCommerce, you can download it here from the WooCommerce site. You can also download it from the Plugins directory on the wordpress.org site. Or alternatively, I provided a copy in the notes for this course. And here is the WooCommerce code reference. And if you want to find out more about the different aspects of WooCommerce, such as here we’ve got the classes. And then if we scroll down, we got interfaces, and all sorts of other things. So here on the left, this is all listed out as well. So you can find things by going down on the left. And another thing that I find really useful up here is the Hook Reference. So I use the action and filter hooks in WooCommerce a lot when I’m customizing it. And I tend to use those more than the classes. So let’s take a look at the code for WooCommerce. So here’s the code for the WooCommerce plugin. You can see there’s a PHP file here in the main folder called woocommerce.php. And that’s the file that runs the plugin and kicks it off in WordPress, but there’s also a lot of other files. There may be hundreds, I’m not sure, I’ve never counted them. But anyway, they’re very well organized, because there’s so many that’s really important. So here we’ve got an assets folder. And within that, you’ve got CSS, fonts, images, and scripts. And if I look into each of those, there are subfolders as well. So it’s all organized in a hierarchical way, so that you can find what you’re looking for really easily. This is a little bit more random with the images, but that’s because there aren’t so many of them. And you can see there’s lots of CSS as well. So those are the assets, there’s also all of the files for internationalization. And you can see here, you’ve got all the states that it’s internationalized for, that it’s localized for. And then we have the include files. And again, there’s subfolders under here. So in the includes, we’ve got abstract, admin, the API, that’s the WooCommerce REST API. We’ve got classes which for some reason I can’t fathom, all entered in a subfolder of their own. We’ve got CLI, the Command Line Interface, customizer, data-stores, and so forth, and I don’t need to read them all out. And then here, we’ve got functions. So the functions files are the ones in which you’ll find these hooks, and the functions that are hooked to them. And those are the ones that I tend to delve into the most. And identify how I can create my own versions of them in my own plugins that I’m using to customize WooCommerce. Now I’m not teaching you how to customize WooCommerce in this course. All I’m doing is teaching you how a plugin is organized with reference to a big complex plugin like WooCommerce. So your plugin won’t have as many files or as many subfolders in these directories as we’ve got here. But it will have a very similar structure. And you might decide not to have a top level assets folder here. You might just have CSS, fonts, images, and scripts up at the top level and that’s up to you. And we’ll be looking at structure in your plugin, and I’ll show you how to do that later on in the course. So let’s take a look at the code, so you can see how it works through. So here we’ve got the main woocommerce.php plugin file. Now that’s actually very small compared to a lot of plugin files, we’ve only got 42 lines of code. But what it does do is it includes here this class-woocommerce.php. And it also returns an instance of WooCommerce. So let’s have a look at this include. And again, we’ll be going into includes a bit later on in this course in more detail. And I’ll demonstrate how to code an include. So we’ve got includes, class-woocommerce.php. So I go into here, I scroll down, and we’ve got class-woocommerce.php. So this is a much bigger file. And that include file effectively dumps all of that code into this spot here in the main plugin file. So if you were to remove all of the include calls in a big plugin like WooCommerce and put all of the code into one file. You would have a huge file with hundreds and hundreds of lines of code, which would actually be quite difficult to interrogate and to work with. Whereas splitting it up like this, makes it easier to manage your code and to find things straight away. So here, you can see we’ve got functions, we’ve got all sorts of things. I’m not going to go through it in detail. But again, we’ve got a lot more includes. So we’ve got interfaces, abstracts, and then core functions, data stores and so forth. And all of these are being included here. So once again, as you can see, it’s the likely effect of dumping all of the code from all of those files right in this file. So this is where all of the action goes on, this is where everything starts to happen. So you can see from this example that WooCommerce and let me close that up. WooCommerce uses a hierarchical structure to split up code, store it in includes to also store assets. And to then call those includes at the time in the code that it needs to do so. That’s how you would organize your own plugins. And even when you’re starting with a small plugin, it’s a good idea to organize it well. Because as you build on your plugin and as you add more code to it in the future, you’ll be glad that you did so. And it also is a way of building up good habits for the way that you organize and manage your plugins. So that’s how a plugin is structured. In the next part of the course, we’re going to take a look at the plugins API. See you next time, and thanks for watching.
In this lesson, I’ll give you an overview of the WordPress APIs you’ll use to create your own plugin.
Related Links
Hello and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, we’re going to look at the plugin API. And that consists of the filter and action hooks that are provided by WordPress. And you can also add to those by creating your own filters and actions. So here, the plugins API page on the Codex details what actions and filters are, and actions and filters, do two different jobs. An action hook is an empty hook which is either one of these actions provided by WordPress and there’s loads more. In fact if I go here, and I click on Action Reference, so here we go, we’ve got all of the action hooks provided by WordPress. And this lists them in the order that they run, so you can identify which ones you need to use at different stages in the process of loading your site. And you see there are hundreds of them, and there are some that you’ll use a lot more than others. And generally, if you’re using a function and you look that up in the Codex, you’ll find that that gives you information about what hook you need to hook function to that includes that WordPress function. So these are the action hooks, now what an action hook is, is a completely empty hook that you can add your own code to. So it’s a bit like those includes that I showed you in Woo commerce before. It’s somewhere where you pull in code that’s somewhere else. So in this case, it’s an action hook provided by WordPress and it’s pulling in code that you add to your plugin. So you would hook your functions to an action hook in order to get the code within your function to run at that particular point in the process, or that particular point in the code. A filter hook is slightly different, so let’s just go back and look at the filters, Filter Reference, here we are. So again, there’s hundreds of them and a filter is slightly different. A filter hook goes around existing code. So if there’s some existing code, for example, some static text, or some sort of functionality. You can attach your own function to that filter hook in order to modify the code that’s already in that filter. So essentially, you’ll replace what’s already within that filter with your own code. So, here is a Tuts+ guide to the difference between actions and filters. And here you got a definition of each of them, and some examples of them. So here I’ve got a function being called directly, we don’t need to worry about that, and here we’ve got hooking a function to an action. Now, there are two functions that we use here. The first one, do_action, creates an action hook within your theme or your plugin. So you can register, you’re federally registering a hook that you can then attach code to by hooking your functions to that action hook later on. So say, in my theme, I might have an action called compass_in_footer. And compass is the prefix I use on the parent theme that I’ve developed, the theme framework that I use for my own themes. So here, I’ve got an action hook called compass_in_footer that I’m adding to my theme in the footer file. And then if I want to put some code in that spot, I can create a plugin and then use add_action, with the first parameter being the name of that action hook and the second parameter being the name of my function. So that effectively adds all the code within my compass_colophon function, into this spot here in my footer file, and I can add as many as I want. And you can also prioritize them. So you can add, for example, here we’ve got 20, the default is 10 if you don’t add a third parameter. But if I have 20, that will fire after any other functions that have been run with the default priority of 10. Or alternatively, I could use 5 or a lower number in order to run it first, and let’s just scroll down to look at filters. So here is an example of how you create a filter. So instead of do filter, which just doesn’t exist, you use the apply_filters function. So the apply_filters function has two parameters, the first is the name of the filter and then the second is the default code that’s going to run unless somebody creates a function and hooks it to that filter. So if nobody creates a function hooked to that filter in a plugin or elsewhere in the theme, that means that this code within my theme will run. But if I want to modify that, I can create a function within my plugin or my functions file. And I can hook it to that, which I would do by scrolling down here, you see the example, and here I’ve used add_filter. And add_filter works in the same way as add_action. The first parameter is the filter hook that I’m adding it to and the second is my function. So my function here is new_credits and this here is the name of the actual filter hook. So that would replace any code in my filter hook in the second parameter with the code in my new function here. Let’s take a look at some examples in some of the Tuts+ courses that have already been created. So here we’ve got a course called 3 More Practical Projects to Learn WordPress Pugin Development, and that takes you through the process of creating three plugins. So within this there’s a plugin called tutsplus-section-menus. And there’s also a theme called tutsplus-plugin-development, and that’s been developed specifically for that course. Now here in the theme, in my sidebar file, I added an action hook by using do_action tutsplus_sidebar. So I’ve now got an action hook right in that spot called tutsplus_sidebar. And in my plugin, I’ve added a function here, tutsplus_section_menu, and that here is hooked to tutsplus_sidebar. So my first parameter is the name of the hook, and my second parameter is the name of the function which you can find up here. So that puts all of this code here, it effectively outputs this code in this particular spot in my sidebar file. And I haven’t added a priority, because this is the only function I’m hooking to that, and I just want it to fire with a default priority of 10. But if I was writing multiple functions, I could use priorities to make sure I knew which one was going to fire first. Now let’s take a quick look at woocommerce, because woocommerce is full of hooks. So here is an example, add_action admin_footer, this is hooking to the action that’s provided by WordPress. That’s not a WooCommerce action, because all the WooCommerce actions have the prefix woocommerce. Let’s see if we can find another one. Here’s another one. So we’ve got a function here called wc_cleanup_logs. And that’s being hooked to the woocommerce_cleanup_logs action hook. If I wanted to find out where the action hook was, so I know exactly where that function is gonna be firing. I could interrogate the WooCommerce documentation and find out exactly where it is. And these hooks are good example of what’s called an extensible plugin or extensible theme. Because if you use hooks to add content and to add code, instead of just directly coding that into the theme template files, or into your plugin file, it means that other people can come along and modify or override the code that you’ve already added. So they could attach another function to that action hook. They could override what’s in the filter hook by using add_filter. Or alternatively, they can unhook existing functions from the action and filter hooks. And let me show you how that’s done. So if we go back into this tutorial about action hooks and filter hooks. Here you can see that there are two functions that you can use to unhook a function from an action. There’s remove_action, and there’s remove_all_actions. remove_action has two parameters, the name of the hook and the name of the function that’s hooked to it that you want to unhook. So that function effectively won’t go to that point in the code. Now the priority is only important if there’s already a priority been assigned when it was originally hooked to the action using the add_action function, so you would use the same priority. And if it has got priority, you need to add that again. remove_all_actions, for that same hook, removes any existing actions from that action hook. So that means that your fresh code, that you’re then gonna hook to it will be the only code that runs on it. Filters are very similar. If I scroll down here, you can see that you can unhook a function from a filter. There’s only one which is remove_filter, there isn’t a remove all filters. So remove_filter and once again, the first parameter is the name of the filter hook. And the second parameter is the name of the function that you want to remove from it, so that works in the same way as remove action. So if you want to completely alter the code that’s being output by a third party theme or by a third party plugin, you can write your own plugin and use remove_action or remove_filter to remove that code, and then use add_action or add_filter to add your own. So that’s an overview of the WordPress Plugins API. In particular, looking at actions and filters. In the next part of the course, we’re going to start coding a plugin. And I’ll show you the introductory text, that commented out text at the top that you have to use in order to set up your plugin and make it work properly. See you next time and thanks for watching.
In this lesson, you’ll learn what you need to add to your plugin file to register it with WordPress.
Related Links
Hello, and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, we’re going to start writing a plugin. And I’ll show you how to set up your plugin so that WordPress knows it’s a plugin and can provide information about it in the admin screens. So here are a couple of resources that will help you with that. Firstly, the Codex page on writing a plugin. And secondly, our WordPress Coding Basics: Your First Plugin course. So here’s my site and here’s my Plugins page in the admin. Now at the moment, my plugin isn’t showing up. And that’s despite the fact that I have added the plugin file to the wp-content/plugins folder in my WordPress installation. But the reason it’s not showing up is because I haven’t added the commented out text that I need to, at the top, in order to set up the plugin. So here’s the code, so if you look, I’m in wp-content and then plugins. And I’ve got a plugin here called tutsplus-sample-plugin. And that’s just got one file in it at the moment. Now when you’re creating a plugin, you can either just use one file for your plugin, as I’ve done with this plugin here, for example. Or you can use a folder and put your plugin file within there. And WordPress will automatically find the file that’s got this commented out text, that we’re about to add, and use that as the base file for the plugin. So anything else you add later on, such as include files and scripts and styles, will be caught via that base plugin file. It’s a good idea to use folders because if you want to add more content later on, you can quite easily. But if you don’t use a folder and you want to change later on, you could quite easily put your file within a folder and WordPress would still recognize it. So let’s open my file. Now at the moment, this is an empty PHP file. So I need to add some commented out text to the top to tell WordPress that this is a plugin. So first, I’ll add my opening php tags. I’m gonna need a slash and an asterisk to show that this is commented out text. The first field I use is Plugin Name. Then I’ll have the Plugin URI. So that’s a link to where the plugin is stored. And that could be anything. That could be to your website. It could be to a GitHub repository, which is what I often use, or it could be to the documentation on your site. Or alternatively, it could be to the Plugins page in the WordPress plugin directory. It needs a Description. Making sure I type it correctly. Then we have the Author. The Author U
