I’ve been receiving a lot of requests for a tutorial showing how to write a plugin using Object Oriented Programming techniques, and so I thought I would give it a go while I have a few minutes spare.

What Is OOP?

Writing a plugin using OOP is basically writing your plugin as a PHP class. This can sound a little daunting at first because of the different way you have to hand functions to actions/filters in PHP. Here I’m hopefully going to make it very simple with a few examples.

Our plugin is not going to do anything in particular, instead I’m going to cover the techniques that you need so that, hopefully, you can apply them to any plugin you need to write in the future.

First, let’s setup a basic plugin file. In your WordPress install’s plugin folder create a new file called fakeplugin.php and add the following code to allow WordPress to find your plugin.

/*
Plugin Name: Fake Plugin
Plugin URI: http://return-true.com/
Description: A Fake Plugin To Show OOP Techniques
Version: 1
Author: Paul Robinson
Author URI: http://return-true.com
*/

You don’t need all of this information if you don’t want to provide it, only Plugin Name: is required as long as you don’t plan to list the plugin on the WordPress plugins directory.

If you go to your ‘Installed Plugins’ page in WordPress you should be able to see your plugin listed. Found it? Good.

Now let’s get on with the coding. First we are going to setup a basic class.

Class FakePlugin {
    public function __construct() {
    }
}

What does this do? Well, not much yet but this is generally what you will start with when making any plugin. The __construct is a special method available in all created classes. If you define the construct it will be ran whenever an instance of the class is created. So if your plugin needs to set anything up when it is instanced this is the perfect place to do it.

A good example of that is adding a page to the WordPress menu for your plugin. Let’s take a look at how the __construct will help with that.

Class FakePlugin {
    
    public function __construct() {
        add_action( 'admin_menu', array($this, 'setup_admin_menu') );
    }

    public function setup_admin_menu() {
        add_object_page( 'My Fake Plugin', 'Fake Plugin', 'activate_plugins', 'my-super-awesome-plugin', array($this, 'admin_page') );
    }

    public function admin_page() {
        $html = <<< HEREDOC
<p>This is some content, it will appear on the page.</p>
HEREDOC;

        echo $html;
    }

}

$FakePlugin = new FakePlugin();

Okay, chances are you are confused, but don’t worry about it. I’m going to try and explain what is going on.

Hopefully you’re now familiar with the constructor now, inside it we have our add_action call. I’m not going to explain how hooks (actions/filters) work as that is a tutorial in itself, but one thing that might be confusing is array($this, 'setup_admin_menu'). It basically just asks WordPress to call the setup_admin_menu function, but because we are in a class WordPress needs the context to use when calling, that is what $this is for.

So this is basically what happens. At the end of the plugin we create an instance of our class. This runs our constructor, which triggers our action, which triggers the setup_admin_menu, which adds our page to the WordPress menu. When the page is visited it triggers the admin_page function registered with the add_object_page function… Phew!

Hopefully that makes sense. That is basically it. OOP plugins aren’t actually as difficult as they sound, it just requires you to keep all of your code contained within a class.

This was by no means meant to be a tutorial on OOP itself, more a quick guide on how to use OOP to create a WordPress plugin that actually works, it’s amazing how many plugins I’ve seen that use OOP incorrectly.

If you have any questions about OOP and WordPress, feel free to ask in the comments & I’ll try to answer as best I can. Thanks for reading.