A low learning curve into WordPress plugin development means that there is no one definitive way to build a plugin. A plugin can be as simple as a single file like Hello Dolly
, or it can be structured as complex as needed to cater for various requirements and functionality. The WordPress Plugin Boilerplate aims to provide a standardized, high quality foundation to build your next awesome plugin.
In this first part of the series, we’ll take a deep look into the boilerplate, including how the files and folders are structured as well as code organization of the boilerplate.
Key Takeaways
- The WordPress Plugin Boilerplate offers a standardized, high-quality foundation for building WordPress plugins, with a low learning curve and flexibility for simple or complex plugin structures.
- The Boilerplate’s version 3.0.0 brings major improvements in code organization and separation of concerns within plugins, and it can be easily obtained by cloning the git repository or downloading a zip copy from the GitHub repository.
- The Boilerplate follows recommended WordPress coding and documentation standards, provides a useful starting guide and tools recommendations, and includes five default classes for various functionalities, making it a terrific starting point for plugin developers.
Background
WordPress plugin boilerplate is originally an initiative by Tom McFarlin to provide a standardized, object-oriented approach to building WordPress plugins. Since the boilerplate is built on top of the Plugin API, and following the Coding Standards and Documentation Standards recommended by WordPress, you can be sure that you’re in safe hands. Recent update of the boilerplate to version 3.0.0 brings major improvements to the boilerplate itself, in terms of code organisation, as well as further separation of concerns within your plugin. If you’re already familiar developing your plugin using the previous version of boilerplate (v2.6.2), you might need to take some time to relearn what’s new in the latest version, which includes new folder structure. This boilerplate is no longer maintained by Tom McFarlin himself (at the time of writing), he’s passed the torch to Devin Vinson. The development is still continuing as usual, so if you have any issues, feel free to report one at the GitHub repository. Contributions are also definitely welcomed.Getting Yourself a Copy
The simplest way to get a copy of WordPress plugin boilerplate is by cloning the git repository itself.git clone https://github.com/DevinVinson/WordPress-Plugin-Boilerplate.git
Or you can always download a zip copy from the GitHub repository.
Since version 3.0.0 was released, the plugin cannot be simply copied into your wp-content/plugins
directly. This is because the actual source code of the plugin is not contained in the root directory, but actually resides in the subdirectory called trunk
. Don’t worry about this now, we’ll talk about the new folder structure later on in this article.
Full detailed instructions on how to install the boilerplate correctly is described in the README.md
file. In a nutshell, you can either:
- Copy the
trunk
directory into thewp-content/plugins
folder OR - Copy the whole boilerplate directory, and then create a symbolic link for the
trunk
subdirectory to thewp-content/plugins/<plugin-name>/
. Creating the symbolic link will depends on the operating system that you’re using.
WordPress Plugin Boilerplate
in your plugin list in the admin dashboard, assuming you haven’t made any modifications to the boilerplate itself. Simply activate the plugin and you’re good to go!
Using the Online Generator to Customize the Plugin
Once activated, you’ll have a sort of vanilla plugin that doesn’t really do anything – that is, just yet. You’ll also need to run a search and replace to the whole boilerplate’s codebase, and this process can be very tedious and time consuming. Luckily, there is an unofficial generator built by Enrique Chavez that can automate the process. Simply head to http://wppb.me/ and complete the form located at the bottom of the page to generate a personalized copy of WordPress plugin boilerplate.Folder Structures
Let’s take a closer look on how the WordPress plugin boilerplate version 3.0.0 is organised compared to the old one. As mentioned previously, the actual plugin code is contained in a subdirectory calledtrunk
instead of in the root directory.
This is to follow the standards employed in the WordPress official plugin repository which contains three main directories which are assets
, branches
and trunk
. The boilerplate already provides two of those, assets
and trunk
.
Here’s a complete directory and files contained in the boilerplate, at the time of writing:
|-- plugin-name/
| |-- assets/
| | |-- banner-772x250.png
| | |-- icon-256x256.png
| | |-- screenshot-1.png
| |
| |-- trunk/
| |-- admin/
| | |-- css/
| | | |-- plugin-name-admin.css
| | |-- js/
| | | |-- plugin-name-admin.js
| | |-- partials/
| | | |-- plugin-name-admin-display.php
| | |
| | |-- class-plugin-name-admin.php
| | |-- index.php
| |
| |-- includes/
| | |-- class-plugin-name-activator.php
| | |-- class-plugin-name-deactivator.php
| | |-- class-plugin-name-i18n.php
| | |-- class-plugin-name-loader.php
| | |-- class-plugin-name.php
| | |-- index.php
| |
| |-- languages/
| | |-- plugin-name.pot
| |
| |-- public/
| | |-- css/
| | | |-- plugin-name-public.css
| | |-- js/
| | | |-- plugin-name-public.js
| | |-- partials/
| | | |-- plugin-name-public-display.php
| | |
| | |-- class-plugin-name-public.php
| | |-- index.php
| |
| |-- LICENSE.txt
| |-- README.txt
| |-- index.php
| |-- plugin-name.php
| |-- uninstall.php
|
|-- .gitignore
|-- CHANGELOG.md
|-- README.md
Here’s a little explanation of what each file and folder does:
.gitignore
Provides a sane default .gitignore to most of the stuff that should not exist in your git repository.CHANGELOG.md
Standard changelog of changes to the boilerplate with date of change.README.md
A useful starting guide that lists the installation instructions, among other few other sections such as tools recommendations and credits.assets
This directory contains the recommended resources that you need to provide whenever you decide to publish your plugin to the WordPress plugin repository. All of the images contained in this directory is the recommended resolution for publishing.trunk
This is the actual plugin that you are going to develop. There are a few folders that separate the codebase between admin and public facing functionality. We’ll go through in details of what each subdirectory represents.admin
There are three directories contained within theadmin
directory, namelycss
,js
andpartials
. As its name suggests, all admin facing functionality should be placed here. By default, theplugin-name-admin.js
andplugin-name-admin.css
is enqueued to yourwp-admin
.class-plugin-name-admin.php
will provide generic functionality where you can define your admin specific hooks.public
This directory is pretty much similar to what theadmin
directory has to offer, the only difference is that thepublic
directory should be used to store all of your public facing functionality codebase.languages
A starting.pot
file where you can provide translation functionality with your plugin.includes
This is probably where pretty much all the magic happens. There are five starting classes included by default, which we will discuss later in the next section.LICENSE.txt
A copy of GPL v2 license is included by default.README.txt
A starting point for your plugin README file. This file pretty covers all of the sections that you can further fill in to provide a good plugin page on WordPress plugin repository.plugin-name.php
The point of entry for your plugin. In here, a general plugin file header is included that you can modify to your own liking. Theregister_activation_hook
andregister_deactivation_hook
are also registered in this file if you ever need to include some sort of functionality on plugin activation and/or deactivation.
Included Classes
As mentioned before, there are five default classes provided inside thetrunk/includes
directory. Let’s see what each of these do:
class-plugin-name-activator.php
This class is instantiated during the plugin activation. It only has one static method,activate()
which is registered to theregister_activation_hook
. Use this class whenever you find yourself in need to do something on plugin activation such as creating custom tables or saving default options.class-plugin-name-deactivator.php
The counterpart forclass-plugin-name-deactivator.php
. It also only contains one static method,deactivate()
which can be used to run any functionality during plugin deactivation.class-plugin-name-i18n.php
A starting point for i18n functionality of your plugin. It has one property,$domain
which stores your plugin text domain. This property can be set using the public methodset_domain()
. Finally, the methodload_plugin_textdomain()
in this class will be called whenever the plugin is loaded.class-plugin-name-loader.php
Probably the most important class in the boilerplate. It contains two properties,$actions
and$filters
where all the hooks registered in the plugin will be stored. It provides two simple wrapper functions,add_action()
andadd_filter
that are used to add actions or filters into the$actions
and$filters
properties. This should not be confused with the WordPress defaultadd_action()
andadd_filter()
function as this class does not actually register them directly. All of the hooks will only be registered during another method calledrun()
.class-plugin-name.php
The class that glues all the pieces together. It holds important information about the plugin such as the plugin name and version. Plus, it will load the dependencies using the methodload_dependencies()
which will include all the above four classes and plugin text domain will be set usingset_locale()
method. All admin and public hooks that were previously registered can also be defined here. This class also provides simpleget
methods, such asget_plugin_name()
to return the plugin name,get_version()
to return the current plugin version andget_loader()
that keeps an instance ofclass-plugin-name-loader.php
.
Conclusion
In short, the WordPress plugin boilerplate provides a terrific starting point for plugin developers. It follows the recommended WordPress coding standards as well as WordPress documentation standards so you know you’re starting off on the right foot. Besides, publishing to the WordPress plugin repository is made easier since the boilerplate already provides much of what you’ll need. Once we’re familiar with the code organisation and folder structure, we’ll explore how to develop an actual plugin using the WordPress plugin boilerplate in the second part of the series.Firdaus Zahari
View AuthorFirdaus Zahari is a web developer who comes all the way from Malaysia. His passion revolves around (but is not limited to) WordPress and front-end development.
ChrisBplugin boilerplateplugin developmentWordPresswordpress pluginswppb
Share this article