Skip to content

itumulak/wp-plugin-boilerplate

Repository files navigation

WP Plugin Boilerplate

This is a modern approach (I think) for building Enterprise WordPress plugins. I have consolidated all necessary tech stack that I know off that makes building scalable, enterprise-level plugins that will power your WordPress application.

🚀 Features

MVC Architecture: Separation of Models, Views, and Controllers for maintainability and clean code.
Dependency Injection: This promotes loose coupling, testability, and reusability.
React Frontend: Use modern JavaScript (React) for dynamic UI parts.
Autoloading via PSR-4 standards (Composer).
Hook Management: Organized action and filter registration.
WP REST API: Build robust, scalable RESTful endpoints using the native WordPress REST API framework.
Scalable Structure: Designed with scalability in mind — easy to extend without being limited by rigid architecture.
Unit testing: Built-in PHPUnit setup for testing use cases — helps catch regressions early and ensures code reliability.
PHP Code Sniffers: Enforces WordPress Coding Standards using phpcs to maintain code consistency and improve readability.

📁 Project Structure

wp-plugin-boilerplate/
├── dist/                     # Compiled frontend assets (JS, CSS, images from React build)
├── includes/                 # Core backend architecture (MVC, routing, DI)
│   ├── Blocks/               # Registers Gutenberg blocks
│   ├── Controllers/          # Handles requests, connects models, returns responses or views
│   ├── Interfaces/           # PHP interfaces for loose coupling and consistent structure
│   ├── Models/               # Business logic and data representations
│   │   ├── Data/             # Data transfer objects (DTOs) and simple data structures
│   │   └── DB/               # Establish database connections and query logic
│   ├── RewriteRules/         # Custom WordPress rewrite rules
│   ├── Roles/                # Custom WordPress roles
│   ├── Routes/               # Registers REST API routes
│   ├── Shortcodes/           # Custom WordPress shortcodes
│   └── PluginLoader.php      # Initializes and loads all components from `includes/`
├── Pages/                    # View layer / page entry points (React or PHP-based)
├── src/                      # React source code (uncompiled)
│   ├── blocks/               # Gutenberg blocks
│   └── pages/                # React-based views/pages
├── tests/                    # PHPUnit tests (unit/integration tests for Models, Controllers, Routes, etc.)
├── vendor/                   # Composer-managed PHP dependencies
├── scripts/                  # Scripts for bundling Gutenburg blocks
├── index.php                 # WordPress fallback/index entry
├── wp-plugin-boilerplate.php # Main plugin bootstrap file
├── vite.config.js            # Vite configuration file for bundling
├── .gitignore                # List of ignored files and folders
├── package.json              # JavaScript package manifest (npm/yarn)
├── phpcs.xml                 # PHP_CodeSniffer configuration (defines coding standard, e.g. WordPress)
├── phpunit.xml               # PHPUnit configuration file (bootstrap file, coverage, test suites)
└── composer.json             # PHP dependencies and autoloader config

⚙️ Requirements

  • PHP 7.4+
  • WordPress 5.8+
  • Node.js + npm (for React build)
  • Composer

🛠️ Installation

  1. Clone the repository:
git clone https://github.com/itumulak/wp-plugin-biolerplate.git
  1. Install PHP Dependencies
composer install
  1. Install frontend dependencies and build React:
npm install
  1. Activate the plugin in the WordPress admin dashboard.

🧪 Development

Gutenburg Development

Ensure that your blocks are registered in Includes\Blocks\BlockLoader.php:

<?php
namespace Itumulak\Includes\Blocks;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

class BlockLoader {
	public function init(): void {
		add_action( 'init', array( $this, 'blocks' ) );
	}

	public function blocks(): void {
		// Register your blocks here...
		// new Block( 'blocks/newsletter-sample' );
	}
}

To build and compile:

npm run build

React Development

To build React components (non-Gutenburg blocks), ensure they are registered in vite.config.js.

...
        v4wp({
            input: {
                samplenotes: 'src/pages/SampleNotes/main.jsx', // Add your react components.
            },
            outDir: 'dist'
        }),
    ],
...

To build and compile:

npm run build:vite

Compile plugin into ZIP

composer zip

Detecting code violation and auto-fixes

For checking for formatting issues:

composer phpcs <REPLACE_WITH_PHP_PATH_FILES>

To fix formats and coding violation. For simple formats/violation only:

composer phpcbf <REPLACE_WITH_PHP_PATH_FILES>

By default we are using WordPress coding standard for sniffing out code violation. I have filter out a few rule here. Feel free modify phpcs.xml if you want modify the sniffer rules to meet your objectives.

Unit Testing

To unit test:

composer test

Feel free to include your own unit test to meet your objectives.

Github Workflow (CI)

This project includes a Github Workflow. It simply run unit testing and PHPCS for code violation. Feel free to add more steps.

Running your own local server (optional)

Docker setup

📋 Prerequisites

Clone wp-docker-server:

git clone https://github.com/itumulak/wp-docker-server

[!TIP] Modify values in .env such as ports, DB name, DB password, etc. to your preference.

Run docker-compose.yml.

docker compose up -d

Go to https://localhost:8080 (or what port you have define in .env) and complete the wordpress installation.

Copy the whole WP Plugin Boilerplate folder into your working wordpress docker instance.

cp -r <PATH_OF_WP_PLUGIN_BOILERPLATE> /var/www/html/<PROJECT_NAME_PATH>/wordpress_data/wp-content/plugins/

📋 TODOs

  • Switch to Preact?
  • Improve handling of database table changes.
  • Add GitHub workflows.
  • Refactor and refine codebase for registering hooks.
  • Utilize PHP Code Sniffer to the project. Go away with PSR-4?
  • Add PHP unit test.
  • Provide Gutenburg support.

📜 License

GPL v2

About

This is a modern approach (I think) for building Enterprise WordPress plugins.

Resources

Stars

Watchers

Forks

Releases

No releases published