Hire Gav!

Object oriented plugin skeleton for WordPress

When I start a new WordPress plugin the first thing I do is import my skeleton template. I’m not going to go into a procedural vs object oriented discussion, but if you want to write clean, easy to manage and reusable code then your best bet is to use an object oriented model.

We start by creating a suitably named folder in the ‘/wp-content/plugins’ directory of your WordPress install. After this, create a file called [plugin-name].php and open it up in your text editor.

Plugin header

Next we create a plugin header. This is where we tell WordPress some general information about your plugin. It will then be helpfully displayed in the plugins section of the dashboard. Common information to store here includes the plugin name, the plugin uri, description, version, author and your website uri.

<?php
/*
Plugin Name: Gav’s Blog Skeleton
Plugin URI: https://www.gavsblog.com
Description: Object oriented plugin skeleton
Version: 0.1
Author: Gav de Ste Croix
Author URI: https://www.gavsblog.com
*/
?>

Create and instantiate the class

Now we are going to create a ‘unique’ class name. By creating a unique class name we will avoid having to create unique function names like you would if you were working procedurally. I prefix my classes with gb_ followed by the name of the plugin. For this demo we will be using gb_object_oriented_skeleton.

class gb_object_oriented_skeleton {

}

Next you will have to choose whether you want to instantiate the plugin globally. If you need it beyond the initial scope then declare it globally. In most cases this won’t be necessary.

Global gb_object_oriented_skeleton;

$gb_object_oriented_skeleton = new gb_object_oriented_skeleton();

Adding a constructor

The constructor is the first part of code that gets run when we create an object. This will be the engine room of our plugin, bringing all of our functions together.

public function __construct() {

}

The first thing we will worry about in our construct is enqueueing any required styles and scripts. We now need a second function to hold all of our styles and scripts.

public function enqueue_scripts() {

}

Then we can use add_action() to instruct WordPress to use them.

class gb_object_oriented_skeleton {

    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    public function enqueue_scripts() {

    }

}

Notice that we have to refer to our enqueue function in an object oriented way. The add_action function will accept an array or a string; the string is a direct reference to a function whereas the array allows us to pass an object instance. First the instance ‘$this’ and second the name of the function ‘enqueue_scripts’ within our instance. ‘$this’ refers to the object that we are currently inside.

Note: Public refers to the visibility of the function. It allows the use of the function outside of the instance scope. We could use private or protected to restrict function use but that isn’t necessary here. For more information on this please see the PHP manual.

The finished skeleton

Your skeleton should now look something like this. I’ve added some dummy scripts and styles in an assets folder for ‘completeness’.

Feel free to use this code as a base for your new plugins.

<?php

/*
Plugin Name: Gav’s Blog Skeleton
Plugin URI: https://www.gavsblog.com
Description: Object oriented plugin skeleton
Version: 0.1
Author: Gav de Ste Croix
Author URI: https://www.gavsblog.com
*/

// Create Class
class gb_object_oriented_skeleton {

    // Constructor
    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    // Styles and Scripts
    public function enqueue_scripts() {
        // Styles
        wp_enqueue_style('gb_object_oriented_skeleton', plugins_url('assets/css/style.css', __FILE__), null, '');

        // Scripts
        wp_enqueue_script('gb_object_oriented_skeleton', plugins_url('assets/js/script.js', __FILE__), array('jquery'), '', true);
    }

}

// Add a Global variable if you need to use outside of instantiated scope
Global $gb_object_oriented_skeleton;

// Instantiate
$gb_object_oriented_skeleton = new gb_object_oriented_skeleton();

?>

Join the discussion!

You might like: