Let’s take a look at WordPress plugin structure. Let me make this clear, this is by no means the official way to structure your plugin. In fact, if you took a look at how other plugins are structured, then you’ll find it’s organized a completely different way than what I’m showing. Some plugin developers are lazy and put all their files in 1 folder. This makes it hard to navigate and find what you’re looking for. Other developers create a very thorough and well structured plugin.
We’re going to be working with our template plugin. The point of this plugin is to have a basic template ready to copy and paste when we want to create a new plugin. It saves you time and makes life a whole lot easier.
WordPress Plugin Structure
We’ll have 5 folders in total in our template folder. Create the following folders:
- css – This will hold all your CSS files.
- img – This will hold all your images.
- inc – This will hold all PHP files that are included in your main plugin file.
- libs – This will hold any libraries you’re using for your project. There will be times where you’ll be using someone else’s library and this will be the place where you’ll want to place all their files inside of.
There will be times where you won’t be using any of the folders at all. This can be for any reason. If you happen to not be using a folder, it’s OK to delete it. You’ll be in situations where you’ll want to create extra folders. For example, if you’re creating a very complex admin panel for your users, then it’s probably best to create a folder called admin. You want your WordPress plugin structure to be flexible and easy to navigate.
There are also some files which you should store in the root directory of your plugin such as CHANGELOG.txt, LICENSE.txt, and README.txt. You don’t NEED to have these files, but it’ll be very useful for anyone who downloads your plugin and would like a little more information about your WordPress plugin.
Lastly, another file you can have is uninstall.php. We haven’t created an actual plugin yet and we haven’t talked about what should be done when a plugin is installed. For future reference, WordPress searches for an uninstall.php in the root directory of your plugin when someone decides to uninstall your plugin. These files are great to have in your WordPress plugin structure.
We’ll be talking more about how to uninstall a plugin in a later lesson. In total, this makes 5 files in our root directory of our plugin. I’ll let you decide as to whether or not you should add more files, but for this series, we’re going to keep everything organized and the only files we’ll have in our root directory is our main index.php file and our uninstall.php file. Feel free to comment and tlell me about your own WordPress plugin structure.
Here’s what our current wpTemplate plugin looks like.
Here’s link to a great article on file organization in general.