4 Ways to Set up a WordPress Plugin Boilerplate.

So you’ve got this great idea for a plugin you’d like to build that could potentially impact your WP project or the WordPress global community, but then you quickly realize you need to do some boilerplate set-up before you can start.

As with most new plugin setups, you might find yourself creating a folder, PHP files and associated asset folders to go along with. Or you might decide to copy an existing custom plugin of yours and start renaming the file names, plugin names, class names, methods and so on… All these take around 5 to 10 minutes every time and involve repeating yourself unnecessarily.

Boring! Boring!! Boring!!!

Surely there must be a better way, so we don’t have to repeat ourselves for tasks that can or should be automated. After all, this is one of the holy tenets of good programming: DRY (Don’t Repeat Yourself).

Enter WP Plugin Boilerplate Generators:

  1. WP CLI

The WordPress CLI (Command Line Interface) terminal offers a very easy way for developers to set up a boilerplate plugin right from the command line. All you have to do is run the following script on your terminal like so:

wp scaffold plugin my-plugin

Please note that this script must be run within your WordPress plugins repository else it will throw an error.

2. WP Strap

This open-source project presents an organized and object-oriented boilerplate for WordPress plugin development and testing. The Boilerplate is based on the Plugin API , Coding Standards, and Documentation Standards and can be installed like so:

npx wp-strap plugin

It includes Composer, Codeception (unit/acceptance testing), PHPCodeSniffer with WordPress Coding Standards to validate your code, TravisCI configuration for automatic testing & continuous integration, Webpack 5 for front-end development incl. BabelJS v7, BrowserSync v2, PostCSS v8, PurgeCSS v3, Autoprefixer, Eslint, Stylelint, SCSS processor, WPPot, and more.

Consider this the modern-day tool for facilitating plugin development and testing with an organized, object-oriented structure to kick-start a build-workflow for your WordPress plugins.

3. WPPB.me

This open-source project was started by Tom McFarlin’s WPPB foundation plugin and is now maintained by Devin Vinson. It presents you with a simple form where you can specify your plugin details and generate a zipped folder containing the plugin files.

WPPB.me Boilerplate

4. Composer

Composer is a PHP dependency manager tool (similar to NPM, Yarn) that helps us manage PHP dependencies and applications. Using Composer allows us to set up a lightweight WP plugin that is void of redundant code or scripts we may not need from previous boilerplate approaches.

composer init --type=wordpress-plugin

You’ll be prompted with a series of questions on the name of your project, license, author, etc. If you’re unsure about any of them, you can simply press enter to go with the default value. You can always change everything later by editing your composer.json file directly.

Your composer.json will typically look like this:

{
"name": "chigozieorunta/my-plugin",
"description": "A simple plugin to illustrate Composer boilerplate setup.",
"type": "wordpress-plugin",
"license": "GPL",
"authors": [
{
"name": "chigozieorunta",
"email": "chigozieorunta@yahoo.com"
}
],
"minimum-stability": "dev",
"require": {}
}

Next, specify in the composer.json, the source path for your plugin code (this is where you intend to put all your plugin logic).

{
"name": "chigozieorunta/my-plugin",
"description": "A simple plugin to illustrate Composer boilerplate setup.",
"type": "wordpress-plugin",
"license": "GPL",
"authors": [
{
"name": "chigozieorunta",
"email": "chigozieorunta@yahoo.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"MyPlugin\\": "src/"
}
}

}

Now generate your Autoload files. This will help us manage namespaces which will, in turn, help us prevent plugin name conflicts with other plugins. To do this, run this on your terminal:

composer dumpautoload -o

Next, you can install other 3rd party PHP packages needed for your plugin development from Packagist. To do this add the PHP packages you need to your composer.json file on the “require” line:

"require": {},

and then, run Composer Install

composer install

Once that is done, you should have a vendor directory folder and a composer.json file which are the bare essentials for your plugin development.

Next, create a “src” directory within the root of your plugins directory. We will now test our new namespace defined on the composer.json file. Go ahead and create a file called HelloWorld.php in the newly created “src” directory like so:

<?phpnamespace MyPlugin;class HelloWorld {
private $message;
public function __construct() {
$this->message = "Hello World!\n";
}

public sayHello() {
return $this->message;
}
}

Now create an index.php in the root directory of your plugin folder like so:

<?phprequire_once __DIR__ . '/vendor/autoload.php';$message = new MyPlugin\HelloWorld();echo $message->sayHello();

OR you can write it this way:

<?phprequire_once __DIR__ . '/vendor/autoload.php';use MyPlugin\HelloWorld;$message = new HelloWorld();echo $message->sayHello();

Run your index.php file on your terminal like so:

php index.php

You should get a printed message on your terminal saying:

Hello World!

And that’s it!!!

Hope this helps you as a WordPress developer. Now go be Awesome!

Full Stack Engineer