WordPress plugins are unbelievably versatile and easy to write. Whether your using a mulit-site instalation or just a normal blog there are only a few small rules that you need to obey.
You can either place your plugin as one single file in /wp-content/plugins/ or, if your plugin is going to have a number of files then place them in /wp-content/plugins/my-plugin-dir. As we are going to be using more than one file for our plugin we are going to use the 2nd option.
Now to think of a good plugin to use for this tutorial. It will have to need shortcodes, ajax and have different admin menus for a user’s blog and for the network admin. It is easy to forget that your plugin will be running along any number of plugins on any number of installations. In order to avoid conflict with other plugins the use of classes is highly recommended. The only time you shouldn’t use classes is if your plugin is only a few lines of code.
For those that haven’t used classes before, they can seem daunting at first, but have no fear like everything in programming – once the penny drops you’ll wonder how you couldn’t understand it in the first place. For a basic, very very quick, lesson on objects and classes – A class is a definition of a function that has functions inside it. The object is the variable that this class is assigned to. The functions inside this ‘main’ function (class) are called methods. I will get bombarded by loads of people telling me that I shouldn’t use this analogy, but like I say this is not a lesson on classes.
As our plugin is going to be installed independent of the theme, we need to keep all our files in the one place – our plugin directory:
/wp-content/plugins/my-massive-plugin/
We are going to have html, js, css, images, admin pages, network admin pages and classes. So here is the directory structure:
/wp-content/plugins/ | |---/my-plugin-dir/ | |---/application/ | | | |---/includes/ | |---/modules/ |---/public_html/ | |---/css/ |---/images/ |---/js/
So an explanation of each:
/wp-content/plugins/my-plugin-dir/
In here you only need your index.php file. This will have the tags for wordpress to know what your plugin does, it will construct your objects, register hooks, init calls, admin pages etc.
/wp-content/plugins/my-plugin-dir/application/
This is where you will be putting your main plugin class.
/wp-content/plugins/my-plugin-dir/application/includes/
Initially I was placing javascript 3rd parties in /public_html/js and php 3rd parties in /application/includes/ but it started to become messy. Some 3rd parties you may use will have both js and php, and when you come back to it later it can be a headache. So ALL 3rd party stuff you may use, like jquery plugins, php classes etc will go here
/wp-content/plugins/my-plugin-dir/application/modules/
This will hold ‘sub-classes’ or modules. In our example we will have 3. A different class for the front-end, the dashboard and the network admin.
/wp-content/plugins/my-plugin-dir/public_html/
Place the ‘view’ files here. These will be plain html files where we will place shortcodes (explanation later) that we can manipulate
/wp-content/plugins/my-plugin-dir/public_html/css/
hmmm… should be self explanatory
These are your own css files, one for each view file, remember any 3rd party css files will go into /wp-content/my-plugin-dir/application/includes
/wp-content/plugins/my-plugin-dir/public_html/js/
This is the same as above, place only your javascript files here.
/wp-content/plugins/my-plugin-dir/public_html/images/
And once again, place only your images here.
The next mission is the naming scheme. This will name your classes, your html files, css files and scripts. What I tend to do is use camel case naming scheme, so for the plugin my-plugin-dir I would use MPDir or as its short WPMyPlugin. Camel case can look ugly at first, I hated it a few years ago, but it has now grown on me. The main rule of thumb when choosing a name is that its:
1) short
2) unique as possible
For this example I’m going to use
WPMyPlugin
. Its short enough for me and should be unique enough for an example plugin.