Creating A WordPress Plugin Using OOP Techniques

/ Wordpress / by Paul Robinson / 3 Comments
This post was published back on April 21, 2013 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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.

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.

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.

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

Author’s gravatar

add_object_page() has been deprecated. Please change it to add_menu_page(). This page is on Google. People might be use deprecated function.

Reply
Author’s gravatar author

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.

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more