Contact!

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:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!