Creating A WordPress Plugin Using OOP Techniques
Today we are going to look at how to write a WordPress plugin using Object Oriented Programming techniques.
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 handle functions to actions/filters. 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 load your plugin.
1 2 3 4 5 6 7 8 |
/* Plugin Name: Fake Plugin Plugin URI: http://return-true.com/ Description: A Fake Plugin To Show OOP Techniques Version: 1.0 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. Don’t activate it quite yet.
For now let’s get on with the coding. First we are going to setup a basic class.
1 2 3 4 |
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 when using OOP. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Class FakePlugin { public function __construct() { add_action( 'admin_menu', array($this, 'setup_admin_menu') ); } public function setup_admin_menu() { add_menu_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_menu_page
function… Phew!
Why Use OOP?
That’s a good question. Arguments can be made for increases in speed, but I use it for one main reason. It helps keep your plugin code organized & practically eliminates function name conflict issues. Because your functions are contained within a class, even if your function name is the same as another one loaded within WordPress, it will not cause an error.
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.
3 Comments
Israel
Thank you, very simple and useful.
Boray Eris
add_object_page() has been deprecated. Please change it to add_menu_page(). This page is on Google. People might be use deprecated function.
Paul Robinson
Hi Boray,
Thank you for your comment.
While I appreciate that the function has been deprecated, this post is also from 2013 and I cannot (and should not) always go back and remove references to deprecated functions. As a reader and consumer of content you should always make sure to do your due diligence and vet code you see online for updated procedures and old functions.
I have changed it this once, because of your comment, but I will not always do so. I’ve also added a notice when a post is older than 1 year because code changes quickly.