We'll create fresh WordPress site with Plugin Builder installed. You have 20 minutes to test the plugin after that site we'll be deleted.
This is a plugin for WordPress plugin developers. If you don’t understand what PHP
, HTML
, CSS
, add_action
and apply_filters
are then this plugin is not for you!
The WordPress Plugin Boilerplate is a fantastic tool for standardising WordPress plugins, and encouraging developers to use best practices. But manual editing of every file to enter the name of the plugin, the author name and all the other metadata is a bit of a chore. This plugin automates that process, giving you a simple form to enter your metadata, then when you hit the ‘Build’ button your new plugin is created with all the correct metadata.
But there’s more. You can also define Custom Post Types and even other custom classes which will have .php files created automatically, and all the relevant code to include those files in your plugin. Once your plugin is built you can, of course, edit the files in any way you want to add extra methods.
Plugin Builder will even create a manager class for your Custom Post Types if you want, as a place to store methods related to your CPTs – such as a get_all() method, for example.
And that’s not all. Some developers like to include extra items in their plugins, like utility classes or frameworks such as the WordPress Settings Framework. Plugin Builder automates that process, too, by downloading and including the files you choose in your new plugin.
Plugin Builder comes with a range of these additional includes you can choose from, but if there are other items you want to include you can make those part of the build process really easily (by extending a simple interface and using a filter).
So, let’s look at the traditional way of using the WordPress Plugin Boilerplate to create a new plugin:
Or, using Plugin Builder:
My guess is Plugin Builder will save you 2-4 hours work, and make your plugins much more standard in their architecture.
When your plugin is built the settings for it will be saved so you can rebuild your plugin at any time (this will overwrite any changes you’ve made manually) or make a few changes and create a new plugin.
The optional includes available in Plugin Builder are:
The WordPress Settings Framework aims to take the pain out of creating settings pages for your WordPress plugins by effectively creating a wrapper around the WordPress settings API and making it super simple to create and maintain settings pages.
util.php is a collection of useful functions and snippets that you need or could use every day, designed to avoid conflicts with existing projects.
Expect more includes to be bundled with future versions of Plugin Builder. If you have an idea for an include you want and you think it may be useful for other developers let me know. Or, add your own include (see the next section for details).
Adding your own includes to Plugin Builder is really easy. There’s an interface named Plugin_Builder_Include which you need to extend, it has a few methods that need to be implemented. Then you add a call to a method in your include class for the plugin_builder_includes filter. Here’s a simple example:
class My_Include implements Plugin_Builder_Include {
/**
* The method run when the user has selected this include.
*
* @since 1.0.0
* @var Plugin_Builder_Settings $settings The settings for the plugin being built.
*/
public function process_include( $settings ) {
// This is where you would do the work for your include; downloading files and saving them locally,
// creating folders, adding settings etc.
// Returns 'true' if your processing succeeds, and a string detailing the error if it fails.
// We ALWAYS use the WordPress Filesystem API for file operations, see this for details: https://codex.wordpress.org/Filesystem_API
global $wp_filesystem;
if ( ! isset( $wp_filesystem ) || null == $wp_filesystem ) {
return false;
}
// get the file we want to include in the new plugin from a local folder
// Note: it's tempting to get your files from somewhere on the Internet (such as Github or your own site) but
// this would be against the WordPress Plugin Guidelines: https://wordpress.org/plugins/about/guidelines/
// Also you can't include your files in a Zip or other archive, they have to be diff-able (i.e. text-based) files.
$cache_file = PLUGIN_BUILDER_DIR . 'cache/my-include.php';
// copy the file to the build folder
$wp_filesystem->copy( $cache_file, $settings->build_path . 'includes/my-include.php' );
return true;
}
/**
* Returns any code to be injected into the top of the load_dependencies() method.
*
* @since 1.0.0
*/
public function get_dependencies_code() {
// Be careful here: you're writing PHP code as a string to be injected into a .php file, so it must be valid.
return "
/**
* My Include
*
* My Include is an example include for the Plugin Builder plugin.
*
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/my-include.php';
";
}
/**
* Returns the unique slug for this include.
*
* @since 1.0.0
*/
public function get_slug(){
return 'my-include';
}
/**
* Returns the translated title for this include.
*
* @since 1.0.0
*/
public function get_title() {
return __( 'My Include' );
}
/**
* Returns the translated description (HTML allowed) for this include.
*
* @since 1.0.0
*/
public function get_description() {
return __( '<p>My Include is an example include for the Plugin Builder plugin.</p>' );
}
/**
* Returns the URL giving more information on this include.
*
* @since 1.0.0
*/
public function get_info_url() {
return 'https://some-website.com/my-include/';
}
/**
* The method that will be called when the add_includes filter runs.
*
* @since 1.0.0
* @var array $includes The array of includes.
*/
public function add_includes( $includes ) {
return $includes;
}
}
// add this include to Plugin Builder using the plugin_builder_includes filter
$my_include = new My_Include();
add_filter( 'plugin_builder_includes', array( $my_include, 'add_include' ) );