When it comes to WordPress plugins, it’s all pretty simple, right? Just a directory, a PHP file with the same name as the directory, the header for the file and it’s just another function.php file, right?
But what if there was another way to organize and code your plugin? What if you tried object-oriented programming?
What is Object-oriented programming?
So what is object-oriented programming all about anyway? Well, OOP, as we’ll call it the rest of the way, does all of the computations inside of objects. And the objects communicate and work with each other to execute the program.
To understand objects, we’ll briefly them and classes. The classic example used for objects is a car. A car has a make and model, number of wheels, an engine, number of doors, etc. And different cars have a different value for each of those attributes.
A class is the blueprint for an object. So when we create a class, we set up the code for those attributes and functions/methods, like startCar or stopCar. Then when we create an object from the class, we define the values for the attributes.
So then with OOP, we’re essentially creating a bunch of objects that a job to do, like hold data for a post or run all of the actions and filters we need for our program. And they all work together to be a complete program.
Sound complicated? It can be, especially when you first start out.
Do you need a new website for your small business?
So you’re thinking about creating a new website for your business. Great. But before you really do anything, let’s sit down and answer this one question: do you really need that new website? There are some situations where a new site would be awesome. But there are also other situations where it’s no benefit to […]
What is procedural programming?
On the other hand, procedural programming is all about executing tasks in the order that they are typed into a program. Essentially, it goes down through the file and executes the functions and programs in order. It’s sort of like a step-by-step guide that you might use.
To see procedural programming in WordPress, you can look no further than the functions.php file of any theme. The vast majority of themes in the WordPress sphere use procedural programming. You can see how the computer/processor would go through the each of the functions and run them when ever the functions.php file is called.
This programming paradigm, or approach to the programming process, is really easy for beginning developers. It’s all intuitive and easy to follow. It’s similar to how we behave as humans, just following a list of items. You don’t need to worry too much about what’s being called here or there. It just works.
Why object-oriented programming in WordPress plugins?
So now you’re probably asking why using object-oriented programming in WordPress plugins is a good thing if it’s so complicated. Well, the answer is a bit, well, complicated.
If you’re just starting out learning PHP and programming, stick with procedural programming for the time being. You really need to shore up the basics before you jump into the deep end that is OOP. And once you feel confident about your skills, learn OOP outside of the WordPress sphere until you feel comfortable with it.
But if you’re more advanced, OOP becomes much easier and clearer to use. By this point, you probably know what you’re doing and what to look for.
And once you get the hang of OOP, it really helps organize things so you know exactly where to go if something goes wrong or where to place added functionality.
For example, Sports Bench, my sports league plugin, has OOP for a lot of the data. There are classes for players, teams, games, playoff brackets and playoff series. This makes it so much easier because when I need to get a player, team, game, etc., I can just call that class, create an object and display data without writing duplicate code for MySQL calls.
So if you’re comfortable with PHP and programming, it might be time to give OOP a try in your next WordPress plugin.
Tutorials on object-oriented programming with WordPress plugins
So how do you implement object-oriented programming into a WordPress plugin? Well, it’s complicated, like a lot of things. But here’s a list of tutorials for you to browse and learn how to implement OOP in a WordPress plugin.
- Object-Oriented Programming in WordPress (Tuts+)
- An Introduction to Object-Oriented Code for WordPress Plugins (WPMU DEV)
- Getting Started with Object-Oriented WordPress Plugin Development (Codementor)
- A Complete Guide to Object-Oriented PHP for WordPress Developers (WPShout)
- WordPress Plugins: Procedural or OOP? (Steve Grunwell)
So if you’ve never tried using OOP with a plugin, I highly suggest you give it a go. At the very least, it’s a great learning experience.