This modern boilerplate template uses namespaces for PHP and React for plugin settings.
Before you begin, ensure you have met the following requirements:
- You have installed the latest version of Node.js
- You have installed the latest version of NPM (included with Node.js)
Copy the contents of the bsd-plugin-boilerplate folder into your \wp-content\plugins folder. The steps below will walk you through customizing these files for your new plugin.
First, we need to replace our generic plugin name and slug with your plugin name and slug.
- Rename the root folder to your plugin's slug.
- Rename
bsd-plugin-boilerplate.phptoyour-plugin-slug.php - Rename
includes\class-bsd-plugin-boilerplate.phptoincludes\class-your-plugin-slug.php - Find and replace all instances of
bsd-plugin-boilerplatewithyour-plugin-slug - Find and replace all instances of
bsd_plugin_boilerplate_adminwithyour_plugin_slug_admin - Find and replace all instances of
BSD_Plugin_Boilerplate_Main_PluginwithYour_Plugin_Slug_Main_Plugin - Find and replace all instances of
BSD_PLUGIN_BOILERPLATE_URLwithYOUR_PLUGIN_SLUG_URL - Find and replace all instances of
BSD_Plugin_BoilerplatewithYour_Plugin_Slug - Find and replace all instances of
BSD Plugin BoilerplatewithYour Plugin Name
Next, install the required Node modules and create a dist file:
- Navigate to
your-plugin-slug\blocksand runnpm install - Run
npm run build. This command creates thedistfolder which contains the JS and CSS files enqueued by your plugin.
Finally, open your WordPress admin dashboard and activate the plugin.
Congratulations! You now have a fully-functional -- if somewhat barebones -- plugin.
Out of the box, you'll have a settings icon in the Admin dashboard:
You can, and probably should, replace the embedded SVG icon in \includes\admins\menu.php. SVG Repo has a great selection of SVG icons.
To start, you'll have a single sample setting available in the settings page:
Obviously, you'll want to replace this with your own plugin option.
Creating your plugin settings requires two steps, and an optional third:
- Register your setting by adding a new call to the
register_settingfunction. Add this call in\includes\init.php, modeling (or updating) the example provided in lines 11-18. - Update
\blocks\app\admin-settings\index.js. Okay, this isn't really a single-step process. Using the boilerplates sample setting as a guide, add your setting as follows:- Create a new constant in
componentDidMount(sample:productVariations). - Add your setting to the settings array inside
setState(sample:BSD_Plugin_Boilerplate_sample_setting: productVariations). - Add a new function to run when your setting is updated. You can copy
sampleSettingsChange(newVal). - Bind your new function (sample:
this.sampleSettingChange = this.sampleSettingChange.bind(this);in the constructor). - Add a new Panel Row in the render function. See the existing
<TextControl>for an example.- Refer to the Block Editor Handbook for components you can use, such as the CheckboxControl, SelectControl, and TextareaControl
- Create a new constant in
- Optionally, but highly recommended, is to clean up after yourself. Add your setting to the array in
uninstall.php
This boilerplate includes scripts that will allow you to package it for deployment. While these are optional -- you can certainly package it by hand -- we highly recommend their use to streamline your plugin process.
- Install gulp globally:
npm install --global gulp-cli
- Add a Gulp script to ZIP only the files necessary for plugin distribution (e.g., ignore
node-modules,blocks\app) - Add a Gulp script to rename files, variables, and constants with your plugin name and slug
- Add PHPUnit for PHP unit tests
- Add React Testing Library for React component testing
