In this mini-series, I’m going to take you through the fundamentals of creating your own WordPress plugin using an object-orientated approach and show you just how easy it really is!

So, lets get started! Start by opening your plugins folder within your WordPress installation and create a new folder called my-awesome-plugin.

Inside this folder, we are going to create some more files and folders. Start by creating your plugin file:

my-awesome-plugin.php

Once you have created your plugin file, we can then create some folders that will be used by the file above:

  • classes
  • images

Now for the fun part, developing your plugin! We need to start with the my-awesome-plugin.php file, this is the key file that will call all other functions into place.

When creating a WordPress plugin you need to provide some details at the top of your main file (in this case, my-awesome-plugin.php), simply use the template below:

/*
Plugin Name: My Awesome Plugin
Plugin URI: http://www.yourwebsite.com/
Description: An awesome plugin I created for WordPress
Version: 1.0
Author: Glenn Taylor
Author URI: http://www.glenntaylor.co.uk/
*/

We now need to create some class files that will be used to create our plugin. Inside the classes folder we created earlier, create 2 files:

install.class.php

plugin.class.php

Now we need to open our key file my-awesome-plugin.php and create some class references, to do this we use require_once to call the class files.

/*--------------- LOAD CLASSES ---------------*/
require_once("classes/install.class.php");
require_once("classes/plugin.class.php");

Now open your install.class.php file and give it its class definitions:

class awesomeInstall{

}

Now open plugin.class.php and do exactly the same as above, except name the class awesomePlugin:

class awesomePlugin{

}

Now return to your key file my-awesome-plugin.php and create instances of both classes we have created above:

/*--------------- CREATE INSTANCES ---------------*/
$install = new awesomeInstall;
$awesome = new awesomePlugin;

Now we need to create the install and uninstall functions in your awesomeInstall.php file, simply open the file and create 2 functions like so:

function awesomeInstallPlugin(){
    add_option('myAwesomePluginOption1','Default value 1');
    add_option('myAwesomePluginOption2','Default value 2');
}

function awesomeUninstallPlugin(){

}

Note the 2 lines that begin add_option, these will be used in part 2 of the mini-series.

We now need to create the hooks that run the functions above in our key file:

/*--------------- INSTALL PLUGIN ---------------*/
register_activation_hook(__FILE__, array($install,'awesomeInstallPlugin'));
register_deactivation_hook( __FILE__, array($install,'awesomeUninstallPlugin'));

IMPORTANT TIP

When creating plugins in WordPress using an object orientated approach, you need to call functions slightly different to the way you would when writing a custom PHP app.

In PHP you would reference a class and a function like this:

$install->awesomeInstallPlugin();

In WordPress when you reference a class and a function, you need to use an array:

array($install,'awesomeInstallPlugin')

This is one area that confuses new plugin developers.

Now we have our activation/deactivation hooks setup in our key file, your file in full should look something like this:

/*
Plugin Name: My Awesome Plugin
Plugin URI: http://www.yourwebsite.com/
Description: An awesome plugin I created for WordPress
Version: 1.0
Author: Glenn Taylor
Author URI: http://www.glenntaylor.co.uk/
*/

/*--------------- LOAD CLASSES ---------------*/
require_once("classes/install.class.php");
require_once("classes/plugin.class.php");

/*--------------- CREATE INSTANCES ---------------*/
$install = new awesomeInstall;
$awesome = new awesomePlugin;

/*--------------- INSTALL PLUGIN ---------------*/
register_activation_hook(__FILE__, array($install,'awesomeInstallPlugin'));
register_deactivation_hook( __FILE__, array($install,'awesomeUninstallPlugin'));

And there you have it! You have successfully created a WordPress plugin, in the next tutorial I will guide you through creating pages for your WordPress plugin and accessing and updating options.

Part 2 now online, click here to read!