Advertisement
  1. Code
  2. Plugins
Code

A Beginner's OOP Class Framework

by
Languages:

OOP is the standard for great code, it is flexible, scalable and extendable. WordPress developers need a framework on which to base their code, something that can be easily ported to other projects to save inventing the wheel twice a week. It's all about working smarter, not harder!

There is nothing worse than activating a plugin to find that it kills your website because two plugin authors have a global function called initialise() !

All code needs an aim, you should never start writing something ( Book, Plugin, Theme or Tutorial! ) without a clear, defined goal of where you want to go with it.

For this tutorial, we are going to use a basic OOP class to do the following in a plugin.

  1. Get links to the child pages of the current page
  2. Save The HTML string in PHP memory to save regenerating the HTML more than once if we need to display the links twice
  3. Define this in a shortcode to save messing around with theme source code

Step 1 Start Creating The Class

Let's start by creating an empty class with nothing but a constructor function and instantiating it into a global variable ( this is really cool, I'll explain later! )


Step 2 Start Delegating Functionality

Then we start delegating functionality from the constructor, define some plugin paths, and create separate functions to add actions to hook into WordPress, all called automatically from the class constructor.


Step 3 Build Out Some Actions

This is looking good so far, it's all setup, but it's really not doing much just yet. Let's keep going on the required functionality.

Child Pages

Setup the plugin enqueues from template_redirect

And the plugin_enqueues function:

Setup a class variable before the constructor ( just to keep it all neat ).

Define the function to retrieve the child page links and set the HTML into the object.

And run it from the template_redirect function


Step 4 Setup The Shortcode And Template Tag!

Child Pages

This is where this starts getting cool, as our shortcode is not going to have any options, we can use what is called a "getter", which simply returns the class variable.

This function can be hooked into the shortcode API like so:

Also, because we have instantiated this all on template_redirect, there is no need to regenerate the links, we can just spit them out anywhere, like this:


Step 5 Pretty It Up

JavaScript Confirm

And the JavaScript snippet. ( This goes in js/init.js in our plugin folder). This highlights the link on hover and asks the user's permission to continue to the child page.

Last but not least, some basic CSS styles for our plugin ( /css/pluginstyles.css ).


Wrap Up

This class can be used in almost any situation, and is well suited ( but certainly not limited to ) for use within the WordPress system. It can be used once for a theme, and then another class setup for some integrated functionality ( meaning we can almost copy / paste the individual functionality from project to project with ease ) and then again for a quick custom plugin for that cool functionality your client needs that is way overdone in the available plugins.

If written to be independent "sections" of the site, then the classes could easily be lifted to your next project, making advanced functionality quick and easy for you!

Although we haven't really even touched on the potential of classes and objects, this is a great start to understanding the flexibility and extendability of OOP.

Also beware that storing objects in PHP like that does take up PHP memory so use thoughfully!!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.