Skip to main content

<Slushman />

unsplash-logo Alain Pham

The Structure of the WordPress Plugin Boilerplate

Published May 29, 2017

In the previous post of the WordPress Plugin Boilerplate series, we looked at why one would use the boilerplate. This post will explore the organized structure in more detail.

The structure makes plugin development more predictable and easier to maintain in the future. The boilerplate organizes files into four main folders:

Plugin Folder

WordPress Plugin Boilerplate Main FolderThe main plugin folder contains basic files for creating a WordPress plugin.

Main Plugin File

This file contains the comments describing what it does, who authored it, the current version, etc. The boilerplate also uses the PHPDoc versions of those same properties.

This file also registers the activation and deactivation hooks, then runs the main plugin file.

readme.txt file

A required file for any WordPress plugin. It describes what the plugin does, who authored it, as well as a changelog and versioning information.

GPL 2.0 license

The boilerplate uses the GPL 2.0 license which is the same license as WordPress. A copy of the license is a wise thing to include in your plugin.

index.php file

The index.php file is internationally blank. Some developers have questioned the necessity of including this file. However, many experienced developers have explained a file like this helps with security concerns.

uninstall.php file

Plugins usually remove settings and other database-related features in the uninstall file.

Languages

WordPress Plugin Boilerplate Languages Folder The languages folder contains all the files needed for translations. The boilerplate includes an empty .pot file for translations. You can optionally include additional .pot files containing the translated strings.

Admin & Public

WordPress Plugin Boilerplate Public Folder WordPress Plugin Boilerplate Admin Folder Since both the admin and public folders have the same structures, I’ll explain both at once. The admin and public classes have the same structure and methods, but keep the different concerns separated.

class-plugin-name-admin.php & class-plugin-name-public.php

This is the main class. Most of the plugin code will reside in one or both of these files.

partials

WordPress Plugin Boilerplate Admin Partials Folder The partials subfolder contains PHP files with the output HTML code. For example, displaying a settings page in the admin requires using two files. The WordPRess API code resides in the admin class, but the HTML output would be in a partial file.

JS

WordPress Plugin Boilerplate Admin JS Folder This folder contains all the Javascript files for the plugin.

CSS

WordPress Plugin Boilerplate Admin CSS Folder This folder contains all the CSS files for the plugin.

Includes

WordPress Plugin Boilerplate Includes Folder The includes folder is Tom’s answer for where to put code that isn’t exclusive to the admin or public. A common question prior to version three was where to put code for features that are both public and admin, like widgets.

class-plugin-name.php

This is the main plugin file. All the plugin’s WordPress hooks register through the Loader class here. All the class files become available throughout the plugin in this file as well.

The constructor sets the class variables. plugin_name is for naming things, like enqueueing stylesheets, and version is for caching busting scripts and stylesheets. Then these four methods run:

class-plugin-name-activator.php

Contains any code that runs during plugin activation.

class-plugin-name-deactivator.php

Contains any code that runs during plugin deactivation.

class-plugin-name-i18n.php

Loads and defines the internationalization files for this plugin so all the strings are ready for translation.

class-plugin-name-loader.php

This is the big new feature of version three. The loader class takes all the hooks defined in the main plugin file and registers them with WordPress.

In the next post, we’ll examine the loader class in detail and how it differs from writing normal WordPress hooks.

Share this post!

Check out these related posts:

Why Use the Boilerplate?

Here are seven reasons why you should use the WordPress Plugin Boilerplate to make WordPress plugins in the second post in the series.

Understanding the Loader Class

Understand the central file for the WordPress Plugin Boilerplate, how it works, how to register hooks and filters, and additional parameters available.

Using the Plugin Generator

Learn how to start your next plugin simpler and faster using the WordPress Plugin Generator, including all the options and what they mean.

Editing the README

The WordPress Plugin Boilerplate include a typically README file needed for any plugin. Explore the eight parts and what to do with them.