- The Boilerplate is based on the Plugin API, Coding Standards, and Documentation Standards.
- All classes, functions, and variables are documented so that you know what you need to change.
- The Boilerplate uses a strict file organization scheme that corresponds both to the WordPress Plugin Repository structure, and that makes it easy to organize the files that compose the plugin.
- The project includes a
.pot
file as a starting point for internationalization.
- Displays a wp-admin error notice to administrators if the required version of PHP is not met
- Displays a wp-admin error notice to administrators if a required third-party plugin (e.g. WooCommerce) is not active
- Easily add a new shortcode by having the shortcode named the same as a method in the
Common
class - Adds a wp-admin Settings page with a link to the plugin's options in the WordPress Customizer
- Includes a custom Sortable Checkboxes control in the WordPress Customizer and examples how to use it
- Includes a number of generally-helpful utility functions, such as getting all public post types, flattening an array of unknown dimensions, and option getters
The Boilerplate can be installed directly into your plugins folder "as-is". You will want to rename it and the classes inside of it to fit your needs.
- Copy this cliff-wp-plugin-boilerplate repository/directory to your wp-content/plugins directory and rename your new plugin's directory
- Delete the
.all-contributorsrc
file - Perform a case-sensitive search and replace at the project level, as follows:
- Rename the
cliff-wp-plugin-boilerplate
directory toyour-plugin-name
. This is your new plugin directory and must match your text domain. - Find the text
cliff-wp-plugin-boilerplate
and replace withyour-plugin-name
in all files - If you will be publishing your plugin to WordPress.org, find the text
Plugin_Data::plugin_text_domain()
and replace withyour-plugin-name
in all files — and then you might not need theuse
import of thePlugin_Data
class and could remove it as well - Find the text
wp_plugin_name
and replace withyour_plugin_name
in all files - Find the text
WordPress Plugin Boilerplate
and replace withYour Plugin Name
in all files - Find the text
WP_Plugin_Name
and replace withYour_Plugin_Name
in all files (the namespace) - Rename the
pot
file underlanguages
and replace the stringcliff-wp-plugin-boilerplate
withyour-plugin-name
- Find the text
https://www.example.com/
and replace with your URI in all files - Find the text
Your Name or Your Company
and replace with your name in all files - Find the text
[email protected]
and replace with your email address incomposer.json
- Find the text
cliffpaulick
and replace with your WordPress.org username (or delete it) inreadme.txt
- Find the text
yourname
and replace with whatever you want as your vendor name) incomposer.json
(such as your GitHub username) - Make other edits to
readme.txt
as appropriate for your own plugin
- Rename the
- Make sure everything in
composer.json
is appropriate to your project.- You do not need
tgmpa/tgm-plugin-activation
if your plugin does not require or recommend any other plugins or themes. - Make sure to update the main plugin file's logic accordingly if you fully remove this library.
- Make sure to update the main plugin file's class properties:
$min_php
$required_theme
$required_plugins
- You do not need
- Run Composer
install
- Activate the plugin
- If it works (as it should), delete THIS README.md FILE
Visit https://getcomposer.org/ to learn all about it.
Here are some quick notes about Composer, in general, and this project's use of it:
- You need to install Composer on your desktop/laptop, not your server. You can download it right into your
cliff-wp-plugin-boilerplate
directory. - The
composer.json
file is the instructions file that tells thecomposer.phar
how to build yourvendor
directory (which includes the autoloader), and possibly do other things. - Run
php composer.phar install
to generate yourcomposer.lock
file. - Because
composer.json
has"optimize-autoloader": true
inside the config key, you will need to run Composer'supdate
if you ever add a new PHP class- See https://getcomposer.org/doc/articles/autoloader-optimization.md for more details.
- It is set this way to lean toward distribution convenience more than development convenience.
- Once ready to build the finalized .zip to distribute to your site or to others...
php composer.phar archive --file cliff-wp-plugin-boilerplate
(name yours correctly)- Because we did not set a
--dir
argument for thearchive
command, Composer will create the .zip right in the project's directory. #Convenient!
- Unzip this newly-created
cliff-wp-plugin-boilerplate.zip
file to make sure it got built correctly (excluding files like .gitignore, composer.json, etc). - Upload this .zip to your production site or wherever you want to distribute it.
- Delete this .zip file from your hard drive.
Following is the pre-built plugin structure. You can add your own new class files (include namespace
and use
at the top) by naming them correctly and putting the files in the most appropriate location.
cliff-wp-plugin-boilerplate/src/admin
- admin-specific functionalitycliff-wp-plugin-boilerplate/src/common
- functionality shared between the admin area and the public-facing partscliff-wp-plugin-boilerplate/src/core
- plugin core to register hooks, load files etccliff-wp-plugin-boilerplate/src/customizer
- WordPress Customizer functionalitycliff-wp-plugin-boilerplate/src/frontend
- public-facing functionality
This plugin requires PHP 7.1.0 or newer and will display a wp-admin error notice if activated in an environment that does not meet this or other requirements (such as required plugins or other dependencies you may code).
For each new version, don't forget to:
- Add a changelog entry to
readme.txt
- Update the version number:
- In your
readme.txt
file's header - In your main plugin file's header
- In your main plugin file's
PLUGIN_VERSION
constant
- In your
- Generate a fresh POT file
The WordPress Plugin Boilerplate uses a variable (e.g. $this->plugin_text_domain
) to store the text domain, used when internationalizing strings.
If you face problems translating the strings with an automated tool/process, replace $this->plugin_text_domain
with the literal string of your plugin's text domain throughout the plugin.
- Here's a discussion from the original project in favor of using variables
- The Plugin Handbook Recommended Way (i.e. not to use variables)
This WordPress Plugin Boilerplate is licensed under GPL version 3 or any later version.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 3 or any later version, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
A copy of the GNU General Public License should be included in the root of this plugin's directory. The file is named
license.txt
; if not, obtain one before using this software by visiting https://www.gnu.org/licenses/gpl-3.0.html or writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
If you opt to use third-party code that is not compatible with this software's license, then you may need to switch to using code that is compatible.
As an example, here's a discussion that states GPLv2-only plugins could not bundle work licensed with Apache 2.0.
The WordPress Plugin Boilerplate was started in 2011, by Tom McFarlin and has since included a number of great contributions. In March of 2015, the project was handed over by Tom to Devin Vinson.
This plugin boilerplate was created by Clifford Paulick in 2018, as a fork of WordPress Plugin Boilerplate with Namespace and Autoloader Support, which forked the WordPress Plugin Boilerplate project to add support for Composer (including autoloading) and namespaces.
Reporting issues -- and especially submitting Pull Requests -- are welcome! Do not contribute if you do not agree to this software's license terms.
Documenting this project's progress...
- Declutter main plugin file by creating new
Bootstrap
class - Now requires PHP version 7.1.0 (up from 5.6.0)
- Added argument type and return type declarations (including scalar, which is why 7.1+ is needed, plus 7.0 was deprecated as of December 3, 2018)
- Refactor classes to be smaller and more intentional, including multiple utilities classes and consolidating settings
- Moved defines and related to
Plugin_Data
class (has static methods because of hard-coded values) - Created abstract
Shortcode()
class, which should be extended when creating your own new shortcodes ([tk_request]
is still included as an example) - Fix
class_exists()
checks to be namespace-aware - Remove unused
libraries
andviews
directories throughout
- Fix
tk_request()
in Common to better support array values
- Added
output_to_log()
utility function to Common to enable writing toWP_DEBUG_LOG
and optionally send an email, such as to the site administrator. - Renamed
wp_plugin_name_get_plugin_display_name()
toget_plugin_display_name()
to remove prefix since we are within our own namespace.
- Add
string_ends_with()
andget_string_between_two_strings()
utility functions.
- Simplify the CSS and JS file names to speed up initial setup by avoiding unnecessary file renaming.
- Simplify boilerplate's repository files so boilerplate can be ran as a plugin itself ("out of the box" as they say), which helps with testing things work before committing changes to the repo.
- Fix logic for Common's
get_option()
andget_option_as_array()
.
- Add link to plugin options screen in the Plugins List admin screen.
- Add plugin options screen that links to WordPress Customizer panel.
- Add methods for getting all Customizer options, deleting all options, and getting a single option (as raw, string, or array).
- Add a custom Customizer Control for multiple checkboxes, optionally sortable. Big thanks to Scott Fennell for the start to the code and permission to use. The version included here is heavily modified and follows this repository's license. Still needs work if wanting to use
<select>
within each checkbox. - Add example Customizer options to help get a quick start.
- Add utility function to detect current URL.
- Add utility function to get public post types, sorted by their labels.
- Tweak -
Common
as class constructor (dependency injection) instead of singleton instance. These articles provide simple examples and explanations if you are curious. - Tweak - Add try/catch around DateTime(), although it shouldn't actually affect code.
- Tweak - Wrap each class within
class_exists()
. - Tweak - Add
ABSPATH
check to top of all PHP files. - Tweak - Remove all
@since
and@access
tags. Remove all@link
tags to the example link.
- Add
flatten_array()
utility method to Common.
- Fix loading logic regarding Admin and Frontend to allow both to run during Ajax.
- Fix to allow Admin hooks to run during Ajax.
- Add ability to require a parent and/or child theme.
- Implement TGM Plugin Activation for required plugins (does not handle requiring a theme). At this time, it does not handle non-bundled premium plugins very well (adding incorrect download links to the TGMPA admin screen), but it does enhance some functionality:
- displaying plugin nice name
- requiring a minimum version number
- adding the ability to mark a plugin recommended without being required
- adding the ability to link to the plugin (the only way to tell people where to download the plugin manually)
- Improve main plugin class' loading, removing static methods and singleton.
Common
class: Use a singleton instead of static methods.- Removed all
@author
DocBlocks, per WordPress' best practices:-
It is WordPress policy not to use the
@author
tag, except in the case of maintaining it in external libraries. We do not want to imply any sort of "ownership" over code that might discourage contribution.
-
- Fix
Common::post_id_helper()
to not return0
when passed0
. Instead, will go through to the logic to automatically determine the Post ID.
- Now requires Composer. See instructions, above.
- Fix
Common::tk_request()
and add new$default
and$escape
parameters.
- Added 'ABSPATH' checks at the beginning of all PHP files
- Added
wp_plugin_name_get_plugin_display_name()
to main plugin file - Removed
PLUGIN_NAME
constant and replaced all usage withPLUGIN_TEXT_DOMAIN
since they were duplicates (as they should have been). Kept it as "plugin_text_domain" in the name instead of something like "plugin_id" or "plugin_slug" to help IDE autocomplete suggestions when using translation functions. - Added
plugin_text_domain_underscores()
to Common
- Added a few nice helper methods to Common
- Improved readme.txt
- Initial Release
Thanks goes to these wonderful people (emoji key):
Stefan Jöbstl 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!