Creating a WordPress plugin is actually very simple. The hardest part is figuring out how to write said plugin. However I have had lots of requests for a basic WordPress plugin tutorial so here you are.
I am aware this tutorial looks similar in parts ofto a previous post I wrote about creating WordPress shortcodes. However this tutorial is specifically meant for creating plugins in general & introduces some of the useful hooks/filters used by plugin authors.
To start out with you need to create a file. I have found it’s best to name it the same as your plugin but hyphenated, or you could use an abbreviation if you have a long name. You can either place it in the plugin folder of WordPress as a single PHP file, or you can make a folder. It’s best to use a folder only if you need to bundle files such as images with it.
Now let’s get onto adding your first piece of code. This is the part that WordPress requires or your file will not be detected as a plugin.
Plugin Name: My First Plugin
Plugin URI: http://return-true.com/
Description: This is my first plugin. Woop, woop!
Author: Paul Robinson
Author URI: http://return-true.com
You will have to change the information to suit you. All of this information is not required for the plugin to be recognized, but I recommend you include it all. Obviously if you don’t have a website for either yourself or the plugin you can omit those.
The Hooks & Filters
Now that you have that information included in your plugin it’s time to start coding. This is the tricky part & since I don’t know what the plugin you are creating is for, I can’t help you. However I can give you some tips such as some of the filters & hooks commonly used when authoring a plugin.
First off is the plugin activation hook. This is (obviously) ran upon the plugins activation. It looks like this:
__FILE__ is a magic constant that always contains the path to the current script. Since the first parameter needs to be the path to the plugin to be activated this constant works great. The second parameter is the name of the function you want to be executed on plugin activation.
An example usage would be if your plugin requires options to be saved you could populate the database with some default options. Other uses might be to check if any old options are there from a previous installation of the plugin & use them as the default options. You get the idea.
This one is pretty obvious. This is the hook that is ran upon a plugins deactivation. It has exactly the same usage as the activation hook, but just in case here is a usage example.
The last one we are going to cover is the hook for inserting code into the head of the users template. It’s actually fairly simple.
This will run the function provided by the second parameter & add the output to the head section of the plugin users template. One thing to note is that the user needs to have
wp_head() in their template, but any decent template normally has it included.
That’s all there is to it. Other hooks can be found on the WordPress codex, the most obvious ones I have mentioned here but there are a few others such as
Remember to check out the WordPress category here on Return True once you have gotten more comfortable with authoring plugins to see how to add more advanced features such as admin pages & even AJAX.