Skip to content

WordPress plugin development — boilerplate to first release

A WordPress plugin needs more than a single PHP file with a header. The boilerplate that gets a custom plugin from scaffold to a first tagged release, with the structure that survives the second feature.

A WordPress agency dev opens a tutorial that shows a plugin as a single PHP file with a header comment and add_action() calls. They ship it. Six months later it’s 1,800 lines of procedural code, no namespacing, function name collisions with a third-party plugin, and zero tests. The plugin works, but every change is a guessing game. A plugin that survives its second feature is structured from the start — PSR-4 autoloading, separated concerns, tagged releases, and a test harness ready to go.

What’s happening

WordPress plugins live in wp-content/plugins/ and need exactly one thing — a PHP file with a plugin header comment. Everything beyond that is a choice. The single-file shape works for genuine one-feature plugins (10-100 lines). Anything bigger needs structure, because WordPress provides none.

The agency-grade boilerplate splits the plugin into:

  • The main file (registration, activation hooks, autoloader bootstrap).
  • A src/ directory with PSR-4 namespaced classes.
  • A tests/ directory with the PHPUnit harness.
  • A composer.json for autoload and dev dependencies.
  • A readme.txt for the WordPress.org repository (even if you never publish there).

The early structure decision pays back the first time you have to add a feature to a plugin you wrote eight months ago.

How to verify

Inspect an existing plugin’s structure to see what you’re inheriting:

cd wp-content/plugins/myplugin
ls -la
# expect: myplugin.php, src/, tests/, composer.json, readme.txt, .gitignore
cat composer.json | jq '.autoload, ."require-dev"'

If everything is in myplugin.php, expect to refactor before adding features. If there’s a src/ with namespaced classes but no composer.json autoloader, look for hand-rolled spl_autoload_register() — workable but not idiomatic.

Check the plugin’s activation behavior:

wp plugin activate myplugin
wp plugin verify-checksums --all

If activation creates database tables, registers cron events, or copies files, those are in the activation hook — confirm they’re idempotent.

What to do

The boilerplate that gets to a first release.

  1. Directory structure:
myplugin/
├── myplugin.php              (main plugin file with header)
├── uninstall.php             (cleanup on plugin deletion)
├── composer.json             (autoload + dev deps)
├── readme.txt                (WordPress.org metadata)
├── .gitignore
├── .distignore               (files to exclude from .zip release)
├── phpunit.xml.dist
├── phpcs.xml.dist            (WordPress coding standards)
├── src/
│   ├── Plugin.php            (main plugin class)
│   ├── Activator.php         (activation logic)
│   ├── Deactivator.php
│   ├── Admin/                (admin-side classes)
│   └── Frontend/             (front-end classes)
├── assets/
│   ├── css/
│   └── js/
└── tests/
    ├── bootstrap.php
    └── test-plugin.php
  1. myplugin.php is the entry point — header, autoloader bootstrap, plugin class instantiation. Keep it small:
<?php
/**
 * Plugin Name: My Plugin
 * Description: Does the thing.
 * Version: 1.0.0
 * Requires at least: 6.4
 * Requires PHP: 8.1
 * Author: Agency Name
 * License: GPL-2.0-or-later
 * Text Domain: myplugin
 */

declare(strict_types=1);

if (!defined('ABSPATH')) {
    exit;
}

define('MYPLUGIN_VERSION', '1.0.0');
define('MYPLUGIN_FILE', __FILE__);
define('MYPLUGIN_DIR', plugin_dir_path(__FILE__));

require_once __DIR__ . '/vendor/autoload.php';

register_activation_hook(__FILE__, ['Agency\\MyPlugin\\Activator', 'activate']);
register_deactivation_hook(__FILE__, ['Agency\\MyPlugin\\Deactivator', 'deactivate']);

add_action('plugins_loaded', function() {
    \Agency\MyPlugin\Plugin::instance()->init();
});
  1. composer.json:
{
    "name": "agency/myplugin",
    "type": "wordpress-plugin",
    "description": "Custom WordPress plugin",
    "license": "GPL-2.0-or-later",
    "require": {
        "php": ">=8.1"
    },
    "require-dev": {
        "phpunit/phpunit": "^9.6",
        "wp-coding-standards/wpcs": "^3.1",
        "yoast/phpunit-polyfills": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "Agency\\MyPlugin\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Agency\\MyPlugin\\Tests\\": "tests/"
        }
    }
}
  1. src/Plugin.php is the orchestrator — singleton, registers hooks, instantiates components:
<?php
namespace Agency\MyPlugin;

final class Plugin {
    private static ?self $instance = null;

    public static function instance(): self {
        return self::$instance ??= new self();
    }

    public function init(): void {
        if (is_admin()) {
            (new Admin\Settings())->register();
        }
        (new Frontend\Shortcodes())->register();
    }
}
  1. uninstall.php is what runs when the user deletes the plugin (not just deactivate). Clean up options, custom tables, scheduled events:
<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}
delete_option('myplugin_settings');
wp_clear_scheduled_hook('myplugin_daily_task');
  1. .distignore controls what ships in the release archive:
/.git
/.github
/tests
/node_modules
/vendor
phpunit.xml.dist
phpcs.xml.dist
composer.lock
  1. Tag the first release. git tag v1.0.0 && git push --tags. If you’re publishing to WordPress.org, use the SVN deploy workflow; for internal distribution, GitHub Releases with the built .zip is enough.

For the testing harness, see WordPress with PHPUnit. For the CI pipeline that builds and publishes releases, see WordPress deployment automation.

Stack Harbor’s enterprise WordPress support practice ships custom plugins with this boilerplate plus CI gating. If a custom plugin is past 2,000 lines in a single file and needs refactoring without breaking the live site, we can scope the project.