An important aspect of keeping your plugin well organised is to create a file and folder structure that makes sense.
I’ve recently done some research on how the leading plugin developers are structuring their plugins, and would like to share this with you.
It seems that most developer favour a Model View Controller (MVC) structure for complex plugins, but I haven’t really developed such complex plugins yet, so I’m not going to go into that. Just for reference, follow this linkΒ to see how anΒ MVC structure typically looks like.
This is the structure I tend to use most when developing plugins:
[php]
my-plugin/
inc/
Any additional plugin-specific PHP files go here
lib/
Library classes, css, js, and other files that I use with many
plugins go here
css/
js/
images/
lang/
Translation files
my-plugin.php
readme.txt
[/php]
If you prefer to have your root directory a bit cleaner, you can use the WordPress Core practice of putting the /js, /css and /images folders in the /inc folder as well.
Here’s another nice structure you can use:
[php]
my-plugin/
admin/
holds all back-end administrative files
js/
holds all back-end JavaScript files
css/
holds all back-end CSS files
images/
holds all back-end images
admin_file_1.php back-end functionality file
admin_file_2.php another back-end functionality file
js/
holds all front end JavaScript files
css/
holds all fronted CSS files
inc/
holds all helper classes
lang/
holds all translation files
images/
holds all fronted images
my-plugin.php main plugin file with plugin meta, mostly includes,action and filter hooks
readme.txt
changelog.txt
license.txt
[/php]
Lets take a look at the structures used by some popular plugins, starting with Easy Digital Downloads:
[php]
my-plugin/
assets/
assets/
css/
images/
js/
includes/
admin/
libraries/
languages/
templates/
my-plugin.php
license.txt
readme.txt
uninstall.php
[/php]
Of course, each developer will have his own preferences and that’s not necessarily a bad thing. For new developers and those seeking guidance on how to structure their files, I recommend the above format. You can build your own template and use it as a start for every new plugin you create.
If you want a ready made boilerplate, check out Tom McFarlin’s WordPress Plugin Boilerplate on GitHub. Here are some of its features:
- The Plugin Boilerplate is fully-based on the WordPressΒ Plugin API
- UsesΒ PHPDocΒ conventions for easily following the code
- Liberal use ofΒ
TODO
Β to guide you through what you need to change - Uses a strict file organization scheme to make sure the assets are easily maintainable
If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.
4 Responses
I also have the same question what Ms.Preethi asked..
Thanks.. I need some code for any plugin with explanation how to create a plugin.. Because i am new to wordpress.. now i need to create a plugin in wordpress.. Can anyone help me with clear explanation of plugin files..
That’s pretty much the same structure I use. I do sometimes have a ‘templates’ directory and ‘admin’ if I have several admin files, other than that it’s the same.
Thanks for your input Chris.