Object-oriented code, among other things, can help organize and add reusability to your code. In this tutorial, I will teach you the basics of writing a WordPress plugin using object-oriented techniques. We’ll be using Dribbble’s API as an example for this tutorial. Ready?
What We’re Going to Learn
- Benefits of using OOP for WordPress plugins
- How to set up a shortcode
- How to set up a template tag
- How to enable shortcode in WordPress widgets
- Real-world example using Dribbble’s API
By the way, if this sounds too advanced and you’re just looking for some ready-made plugins that you can download and use on your site, check out our free course on the best WordPress plugins to use for SEO, backup, security, and more.
Why Use OOP?
Before moving forward with this tutorial, you should have at least an elementary understanding of writing a WordPress plugin. Jonathan has written an amazing tutorial on “How to write a WordPress Plugin“. Give it a read.
Creating WordPress plugins with object-oriented code is quite efficient and tidy when compared to using procedural code. It’s easier to manage the code base and expand it using inheritance techniques, which can be particularly helpful when writing a large plugin.
To write a WordPress plugin, we first need a sense of direction. We’re going to write a plugin which will display the latest shots from Dribbble, using their REST API. We’ll then add shortcode support for posts and widgets, and a template tag for themes.
1. Setting Up the Plugin Class
Object-oriented code is based on classes and methods (functions). Let’s create our core class, which will interact with WordPress hooks and filters.