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

2.2 Plugin Code and Structure

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.

Back to the top