How to Use Plugin Boilerplate in WordPress for Clean Plugin Development
To use a
plugin boilerplate in WordPress, download a standard boilerplate template like the WordPress Plugin Boilerplate, then customize its files and folders to build your plugin with organized code. The boilerplate provides a clean structure with predefined hooks, classes, and methods to help you develop plugins faster and maintainably.Syntax
The plugin boilerplate follows a structured folder and file pattern to organize your plugin code. Key parts include:
plugin-name.php: Main plugin file with header info and loader./includes/: PHP classes and core functionality./admin/: Admin area code and settings./public/: Frontend display code.Loader class: Registers hooks and actions.Activation/Deactivation hooks: Setup and cleanup.
This structure separates concerns and uses OOP for clarity.
php
<?php /** * Plugin Name: Sample Plugin * Description: A plugin built with boilerplate structure. * Version: 1.0.0 * Author: Your Name */ // If this file is called directly, abort. if (!defined('WPINC')) { die; } // Include the core plugin class. require_once plugin_dir_path(__FILE__) . 'includes/class-sample-plugin.php'; // Run the plugin. function run_sample_plugin() { $plugin = new Sample_Plugin(); $plugin->run(); } run_sample_plugin();
Example
This example shows a minimal plugin using the boilerplate pattern with a main class that hooks into WordPress to add a simple shortcode.
php
<?php /** * Plugin Name: Boilerplate Example * Description: Example plugin using boilerplate structure. * Version: 1.0.0 * Author: Your Name */ if (!defined('WPINC')) { die; } class Boilerplate_Example { public function __construct() { add_action('init', [$this, 'register_shortcode']); } public function register_shortcode() { add_shortcode('hello', [$this, 'hello_shortcode']); } public function hello_shortcode() { return '<strong>Hello from Boilerplate Plugin!</strong>'; } public function run() { // Additional run logic if needed } } function run_boilerplate_example() { $plugin = new Boilerplate_Example(); $plugin->run(); } run_boilerplate_example();
Output
When you add [hello] shortcode in a post, it outputs: Hello from Boilerplate Plugin!
Common Pitfalls
Common mistakes when using plugin boilerplates include:
- Not updating plugin header info, causing WordPress to misidentify the plugin.
- Forgetting to load required files or classes, leading to fatal errors.
- Mixing admin and public code in one file instead of separating concerns.
- Not using proper hooks for activation and deactivation.
- Ignoring namespace or class name conflicts.
Always follow the boilerplate structure and test your plugin activation and shortcode or hook outputs.
php
<?php // Wrong: Mixing admin and public code in main plugin file add_action('admin_menu', 'add_admin_menu'); function add_admin_menu() { // admin menu code } // Right: Separate admin code in /admin folder and load conditionally if (is_admin()) { require_once plugin_dir_path(__FILE__) . 'admin/class-plugin-admin.php'; $admin = new Plugin_Admin(); }
Quick Reference
Tips for using WordPress plugin boilerplate:
- Keep your plugin files organized by function (admin, public, includes).
- Use OOP classes to encapsulate functionality.
- Register hooks and shortcodes inside class methods.
- Use activation and deactivation hooks for setup and cleanup.
- Test your plugin in a local WordPress install before publishing.
Key Takeaways
Use a plugin boilerplate to organize your WordPress plugin code cleanly and maintainably.
Separate admin and public functionality into different files and classes.
Always register hooks and shortcodes inside class methods for clarity.
Test activation, deactivation, and shortcode output to avoid common errors.
Follow the boilerplate folder structure to keep your plugin scalable.