|
| 1 | +# WordPress Plugin Boilerplate - AI Coding Instructions |
| 2 | + |
| 3 | +## Architecture Overview |
| 4 | + |
| 5 | +This is a **modern WordPress plugin boilerplate** using PSR-4 namespacing (`WordPress_Plugin_Boilerplate\`) with a custom autoloader and webpack-based asset compilation. The plugin follows a **singleton pattern** with a centralized hook loader system. |
| 6 | + |
| 7 | +### Core Components |
| 8 | + |
| 9 | +- **`includes/main.php`**: Singleton main class that orchestrates the entire plugin lifecycle |
| 10 | +- **`includes/Plugin_Autoloader.php`**: Custom PSR-4 autoloader (replaces composer autoloader for plugin classes) |
| 11 | +- **`includes/loader.php`**: Centralized hook registration system - all WordPress hooks go through this |
| 12 | +- **`admin/` & `public/`**: Separate namespaces for admin-only and public-facing functionality |
| 13 | + |
| 14 | +### Key Architectural Patterns |
| 15 | + |
| 16 | +**Singleton Pattern**: Main class uses `Main::instance()` - always check existing implementation |
| 17 | +**Hook Loader**: Never register hooks directly - use `$this->loader->add_action()` and `$this->loader->add_filter()` |
| 18 | +**Namespace Mapping**: `Includes\` → `includes/`, `Admin\` → `admin/`, `Public\` → `public/` |
| 19 | + |
| 20 | +## Development Workflow |
| 21 | + |
| 22 | +### Asset Compilation (Critical) |
| 23 | +```bash |
| 24 | +npm run start # Development with watch mode |
| 25 | +npm run build # Production build for release |
| 26 | +``` |
| 27 | + |
| 28 | +**Asset Loading Pattern**: Each Main.php loads webpack-generated `.asset.php` files: |
| 29 | +```php |
| 30 | +$this->js_asset_file = include \WORDPRESS_PLUGIN_BOILERPLATE_PLUGIN_PATH . 'build/js/backend.asset.php'; |
| 31 | +``` |
| 32 | + |
| 33 | +### Plugin Creation Workflow |
| 34 | +Use `./init-plugin.sh` to scaffold new plugins - it performs bulk find/replace across all files, updates namespaces, class names, and creates repository structure. |
| 35 | + |
| 36 | +## Code Conventions |
| 37 | + |
| 38 | +### Constants & Namespacing |
| 39 | +- **Global constants**: Use `\WORDPRESS_PLUGIN_BOILERPLATE_*` prefix (note the backslash for global scope) |
| 40 | +- **Class instantiation**: Always use full namespaces or imports |
| 41 | +- **Security**: Every file starts with `defined( 'ABSPATH' ) || exit;` |
| 42 | + |
| 43 | +### Hook Registration Pattern |
| 44 | +```php |
| 45 | +// WRONG - Never register hooks directly |
| 46 | +add_action( 'init', array( $this, 'method' ) ); |
| 47 | + |
| 48 | +// CORRECT - Always use the loader |
| 49 | +$this->loader->add_action( 'init', $this, 'method' ); |
| 50 | +``` |
| 51 | + |
| 52 | +### WordPress Function Preferences |
| 53 | +- Use `untrailingslashit()` instead of `rtrim( $path, '/' )` |
| 54 | +- Use `null === $var` instead of `is_null( $var )` |
| 55 | +- Use `require` (not `require_once`) for asset files that return arrays |
| 56 | + |
| 57 | +## File Structure Rules |
| 58 | + |
| 59 | +### Asset Organization |
| 60 | +- **Source**: `src/js/`, `src/scss/`, `src/media/` |
| 61 | +- **Build**: `build/js/`, `build/css/`, `build/media/` |
| 62 | +- **Webpack config**: Handles multiple entry points for frontend/backend separation |
| 63 | + |
| 64 | +### Component Structure |
| 65 | +``` |
| 66 | +admin/Main.php # Admin-specific functionality |
| 67 | +admin/partials/ # Admin UI components |
| 68 | +public/Main.php # Public-facing functionality |
| 69 | +includes/ # Core plugin classes |
| 70 | +├── main.php # Main orchestrator |
| 71 | +├── loader.php # Hook management |
| 72 | +├── Plugin_Autoloader.php # Custom autoloader |
| 73 | +├── activator.php # Plugin activation |
| 74 | +└── deactivator.php # Plugin deactivation |
| 75 | +``` |
| 76 | + |
| 77 | +## Composer Integration |
| 78 | + |
| 79 | +The plugin uses **dual autoloading**: |
| 80 | +1. **Custom autoloader** for plugin classes (`Plugin_Autoloader.php`) |
| 81 | +2. **Composer autoloader** for third-party dependencies |
| 82 | + |
| 83 | +### Adding Dependencies |
| 84 | +Per README.md, use specific WPBoilerplate packages: |
| 85 | +```bash |
| 86 | +composer require wpboilerplate/wpb-updater-checker-github # GitHub updates |
| 87 | +composer require wpboilerplate/wpb-register-blocks # Block registration |
| 88 | +``` |
| 89 | + |
| 90 | +## Version Management |
| 91 | + |
| 92 | +- **Plugin header**: Update version in main plugin file |
| 93 | +- **package.json**: Keep in sync for npm builds |
| 94 | +- **SemVer**: Start at 0.0.1, use semantic versioning |
| 95 | + |
| 96 | +## Deployment |
| 97 | + |
| 98 | +### GitHub Actions |
| 99 | +- **`build-zip.yml`**: Triggers on main branch push, creates release zip |
| 100 | +- **`wordpress-plugin-deploy.yml`**: Deploys to WordPress.org on tag push |
| 101 | + |
| 102 | +### Build Process |
| 103 | +1. `npm run build` - Compiles assets |
| 104 | +2. Creates zip excluding dev files (via `.distignore`) |
| 105 | +3. Deploys to WordPress.org repository |
| 106 | + |
| 107 | +## Common Gotchas |
| 108 | + |
| 109 | +- **Constant scope**: Use `\CONSTANT_NAME` in namespaced files |
| 110 | +- **Asset loading**: Include webpack `.asset.php` files for dependency management |
| 111 | +- **Hook timing**: Register admin menu on `init`, not `plugins_loaded` to avoid translation issues |
| 112 | +- **Text domain**: Use `load_plugin_textdomain()` on `init` hook, not earlier |
0 commit comments