ashy4n / plugin-template
A WordPress plugin skeleton (boilerplate) for use with composer create-project.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=8.0
- humbug/php-scoper: ^0.20.0
- php-di/invoker: ^2.3
- php-di/php-di: ^7.0
- twig/twig: ^3.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpcompatibility/php-compatibility: ^9.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.7
- symfony/var-dumper: ^6.0
This package is auto-updated.
Last update: 2025-08-27 17:29:18 UTC
README
A comprehensive, modern WordPress plugin boilerplate built with PHP 8+, featuring dependency injection, Twig templating, modern frontend tooling, and best practices for scalable plugin development.
๐ Features
Core Architecture
- Object-Oriented Design: Clean, maintainable architecture using modern PHP 8+ features
- Dependency Injection: Full DI container integration with PHP-DI
- Hookable Interface: Standardized approach to WordPress hooks with
Hookable
interface - PluginData Class: Centralized plugin information management
- PHP 8+ Requirement: Enforces modern PHP features and best practices
Modern Development Tools
- Vite + TypeScript: Modern frontend build system with Hot Module Replacement
- Sass/SCSS: Advanced CSS preprocessing with variables and mixins
- PHPStan: Static code analysis for PHP
- PHPUnit: Comprehensive testing framework
- PHP CodeSniffer: Code quality and style enforcement
- PHP Scoper: Dependency prefixing to prevent conflicts
Templating & Frontend
- Twig Templates: Secure, flexible templating engine
- React Components: Modern JavaScript framework for interactive UIs
- Responsive Design: Mobile-first CSS architecture
- Asset Optimization: Minified and optimized production builds
Testing & Quality
- Unit Tests: PHPUnit test suite with WordPress function mocking
- Code Coverage: Automated test coverage reporting
- Static Analysis: PHPStan integration for error detection
- Code Standards: PSR-12 and WordPress coding standards
๐ Requirements
- PHP 8.0 or higher
- WordPress 5.0 or higher
- Node.js 16+ (for frontend development)
- Composer (for PHP dependencies)
๐ ๏ธ Installation
1. Create a new plugin from this template
composer create-project ashy4n/plugin-template your-plugin-name
cd your-plugin-name
2. Install dependencies
# Install PHP dependencies composer install # Install Node.js dependencies npm install
3. Configure your plugin
Run the setup script to configure your plugin:
composer run setup
This will prompt you for:
- Plugin name
- Plugin slug
- Author name
- Author URI
- Plugin URI
- Description
๐๏ธ Project Structure
plugin-template/
โโโ assets-src/ # Frontend source files
โ โโโ components/ # TypeScript/React components
โ โโโ styles/ # SCSS files
โ โโโ admin.ts # Admin entry point
โ โโโ frontend.ts # Frontend entry point
โโโ assets/ # Compiled frontend assets
โโโ core/ # Core framework files
โโโ src/ # Plugin source code
โ โโโ Hooks/ # WordPress hook classes
โ โโโ Objects/ # Data objects
โ โโโ Services/ # Service classes
โ โโโ Plugin.php # Main plugin class
โโโ templates/ # Twig templates
โ โโโ admin/ # Admin templates
โ โโโ frontend/ # Frontend templates
โโโ tests/ # Test files
โ โโโ Unit/ # Unit tests
โ โโโ bootstrap.php # Test bootstrap
โโโ lang/ # Translation files
โโโ vendor/ # Composer dependencies
โโโ node_modules/ # Node.js dependencies
โโโ composer.json # PHP dependencies
โโโ package.json # Node.js dependencies
โโโ phpunit.xml # PHPUnit configuration
โโโ phpstan.neon # PHPStan configuration
โโโ vite.config.ts # Vite configuration
โโโ tsconfig.json # TypeScript configuration
๐ง Development Workflow
Frontend Development
# Start development server with HMR npm run dev # Build for production npm run build # Type checking npm run type-check # Linting npm run lint
PHP Development
# Run tests composer test # Run tests with coverage composer run test:coverage # Static analysis composer run analyse # Code style check composer run cs # Fix code style issues composer run cs:fix
Building for Production
# Build frontend assets npm run build # Build PHP with scoped dependencies composer run build
๐ฏ Key Concepts
Hookable Interface
All classes that interact with WordPress hooks must implement the Hookable
interface:
<?php use Template\Hooks\AbstractHookable; class MyHook extends AbstractHookable { public function hook(): void { $this->addAction('init', [$this, 'onInit']); $this->addFilter('the_content', [$this, 'modifyContent']); } public function onInit(): void { // Plugin initialization logic } public function modifyContent(string $content): string { // Content modification logic return $content; } }
PluginData Class
Centralized plugin information management:
<?php use Template\Objects\PluginData; $pluginData = new PluginData( name: 'My Plugin', slug: 'my-plugin', version: '1.0.0', // ... other properties ); // Access plugin information echo $pluginData->name; echo $pluginData->getAssetsUrl(); echo $pluginData->getTemplatesDir();
Twig Templating
Secure template rendering with Twig:
<?php use Template\Services\TwigService; $twigService = new TwigService($pluginData); $html = $twigService->render('admin/dashboard.twig', [ 'user' => $currentUser, 'settings' => $settings, ]);
{# templates/admin/dashboard.twig #} {% extends "admin/layout.twig" %} {% block content %} <div class="plugin-admin"> <h1>{{ plugin.name }} Dashboard</h1> <p>Welcome, {{ user.name }}!</p> {% for setting in settings %} <div class="setting"> <label>{{ setting.label }}</label> <input type="text" value="{{ setting.value }}"> </div> {% endfor %} </div> {% endblock %}
Dependency Injection
Services are automatically injected via the DI container:
<?php use Template\Services\ExampleService; class MyService { public function __construct( private readonly PluginData $pluginData, private readonly TwigService $twigService, private readonly ExampleService $exampleService ) { } }
๐งช Testing
Writing Tests
<?php namespace Template\Tests\Unit; use PHPUnit\Framework\TestCase; use Template\Objects\PluginData; class PluginDataTest extends TestCase { public function testPluginDataProperties(): void { $pluginData = new PluginData(/* ... */); $this->assertEquals('Test Plugin', $pluginData->name); $this->assertEquals('1.0.0', $pluginData->version); } }
Running Tests
# Run all tests composer test # Run specific test file ./vendor/bin/phpunit tests/Unit/PluginDataTest.php # Run with coverage report composer run test:coverage
๐ฆ Building for Distribution
1. Build Frontend Assets
npm run build
2. Scope Dependencies
composer run php-scoper
3. Create Distribution Package
The build process creates a build/
directory with:
- Prefixed vendor dependencies
- Compiled frontend assets
- Optimized for production
๐ Security Features
- Nonce Verification: Built-in WordPress nonce support
- Input Sanitization: Proper escaping and validation
- Template Security: Twig auto-escaping
- Dependency Isolation: PHP Scoper prevents conflicts
๐ Internationalization
// Load text domain load_plugin_textdomain($pluginData->textDomain, false, $pluginData->getLangDir()); // Use in templates esc_html__('Hello World', $pluginData->textDomain);
๐ Best Practices
Code Organization
- Use the
Hookable
interface for all WordPress interactions - Keep services focused and single-purpose
- Use dependency injection for loose coupling
- Follow PSR-12 coding standards
Performance
- Use asset versioning for cache busting
- Enable Twig caching in production
- Minimize database queries
- Use WordPress transients for caching
Security
- Always verify nonces for form submissions
- Sanitize and validate all user input
- Use prepared statements for database queries
- Follow WordPress security guidelines
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
For support and questions:
- Create an issue on GitHub
- Check the documentation
- Review the example code
๐ Updates
To update the boilerplate:
composer update npm update
Built with โค๏ธ for the WordPress community