I have written WordPress plugins for over 10 years now.
I have also read and tweaked over a few hundred public plugins to add/remove features, and seen how they are structured.
I know that OOP is considered the golden paradigm for software development, but I am recently using a different approach for plugins, which is procedural.
I am going to explain how I take this approach and I would like to receive as much constructive criticism as possible.
Please don’t just say: “Y*ou should try OOP or read a book about it*”. I will ignore that kind of comments and not waste my energy with them (to the first person commenting that I may answer and say “congrats for the easy joke!”) but then… enough. If you believe you have a good understanding of both sides of the coin (procedural and OOP) please share solid arguments so we can all learn from you.
# How I am developing plugins and why:
**The most basic way to describe it is**: I create single PHP files for each thing the plugin should do or deal with. So I will have the main plugin file which includes all the separate small includes.
To keep this easy to understand, I will go with a real example of a plugin I am working on:
– plugin-newsletters.php
– /includes/
– /includes/general-helper-functions.php
– /includes/register-custom-post-type-subscriber.php
– /includes/register-custom-post-type-newsletter.php
– /includes/custom-cron-schedule.php
– /includes/handle-unsubscribe-user.php
– /includes/send-pending-newsletter-emails.php
– /includes/plugin-activation.php
– /includes/plugin-deactivation.php
And that’s it. Each of those included files has a few functions which are attached to different hooks. For example:
<?php
namespace plugin\newsletters;
add_filter( ‘cron_schedules’, ‘plugin\newsletters\add_cron_schedule’ );
function add_cron_schedule( $schedules ) {
$schedules[‘every_minute’] = array(
‘interval’ => 60, // 60 seconds
‘display’ => __( ‘Every Minute’, ‘newsletters’ )
);
return $schedules;
}
And that same pattern is followed in all other files.
I am not against OOP, but I feel it’s a very stupid thing to throw everything into one class and then instance it as a Singleton with an *init()* and a *run()* method, when I can have everything separated into files that are easily located.
I feel I get things done way faster this way, and I can also reuse parts of my code easily because they are separated in real problem solutions.
* Create a custom post type
* Create some taxonomy
* Register a cron schedule
* Define a metabox for a date field
* Add an automated trigger for an email in whatever specific case
* Anything else that consists of a problem on its own
The only use case I see for OOP here is when I have something that actually should be instantiated into multiple objects. But WordPress already does that natively. When I query posts I get instances of WP\_Post.
This whole thing is in line with another question I asked sometime ago… regarding [if the WordPress architecture is really as bad as they say]).
Thanks in advance for all the wisdom you may share.
[ad_2]