Articles WordPress Development

PHP OOP for WordPress Plugins — Stop Writing Code You’ll Regret in Six Months

September 5, 2026

WordPress core was largely built with procedural PHP, and most tutorials still teach it that way: a functions.php file, a bunch of add_action() calls, global variables passed between functions. It works. For small projects it’s fine. But somewhere around 500 lines of procedural plugin code, things start to get hard to reason about. Functions with similar names. Unclear dependencies. Side effects that happen because of load order.

Here’s the structure I use for every serious plugin I build. Not a theoretical exercise — this is the pattern that’s survived code reviews, team handoffs, and the experience of opening your own code six months later.

The basic plugin class

Every plugin I build starts with a main class that owns the plugin’s lifecycle. The main plugin file — the one with the Plugin Name: header — does almost nothing except instantiate this class:

// my-plugin.php (main plugin file)
require_once plugin_dir_path( __FILE__ ) . 'includes/class-my-plugin.php';
add_action( 'plugins_loaded', array( My_Plugin::class, 'get_instance' ) );

// includes/class-my-plugin.php
class My_Plugin {
    private static ?My_Plugin $instance = null;
    private string $version = '1.0.0';

    private function __construct() {
        $this->init_hooks();
    }

    public static function get_instance(): My_Plugin {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function init_hooks(): void {
        add_action( 'init', array( $this, 'register_post_types' ) );
        add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
        add_filter( 'the_content', array( $this, 'filter_content' ) );
    }
}

The singleton pattern ensures the plugin initialises once. All hooks are registered in one place (init_hooks), so you can see the entire plugin’s behaviour at a glance without grepping through multiple files.

Using namespaces to avoid collisions

PHP namespaces let you name your classes whatever makes sense without worrying about conflicts with other plugins. Add a namespace declaration at the top of each file:

namespace MyPluginCore;

class Plugin {
    // your code here
}

// In the main plugin file:
use MyPluginCorePlugin;
add_action( 'plugins_loaded', array( Plugin::class, 'get_instance' ) );

Now your class can be named “Plugin” without conflicting with any other plugin that also has a class called Plugin. The namespace is your unique identifier. It also makes autoloading with Composer’s PSR-4 standard trivially simple.

Separating concerns with sub-classes

When the main class gets complex, split it. Admin functionality in an Admin class. Front-end output in a Frontend class. REST API endpoints in an Api class. The main plugin class instantiates these and passes them any shared dependencies:

private function init_hooks(): void {
    $assets = new Assets_Loader( $this->version );
    $admin  = new Admin( $assets );
    $api    = new REST_API();

    add_action( 'admin_enqueue_scripts', array( $admin, 'enqueue_scripts' ) );
    add_action( 'rest_api_init',         array( $api,   'register_routes'  ) );
}

Each class has a single responsibility. The Admin class doesn’t know about the REST API. The REST_API class doesn’t touch the admin. When a bug appears in admin behaviour, you open one file. When you want to extend the API, you open one file. This sounds obvious but it’s a significant difference in practice once a plugin gets past a few hundred lines.

Why this matters for maintenance

The real argument for OOP in WordPress isn’t elegance — it’s the experience of opening a plugin six months after you wrote it and being able to understand it in ten minutes instead of an hour. Procedural code that works fine is still harder to navigate at scale. You end up grepping for function names, following a trail of add_action calls, and hoping nothing was added under a confusingly similar name.

OOP forces you to think about structure upfront. That thinking pays back every time someone — including you — has to work inside that code later. For anything beyond a simple snippet, it’s worth the small initial overhead of setting up a proper class structure from day one.