This project allows with a simple bash command to create WordPress plugins already organized in the object-oriented format with the added bonus of a preconfiguration of composer and webpack for more modern plugins using for example scss.
GPL-3.0 License
This project allows with a simple bash command to create WP plugins already organized in the object-oriented format with the added bonus of a preconfiguration of composer and webpack for more modern plugins using for example scss.
This project is based on another Open Source project from DevinVinson: WordPress Plugin Boilerplate.
I modified the initial project structure slightly and added mostly webpack and composer.
As well as a page for plugin settings, which in my experience is useful in 90% of projects, if you don't need it just remove it.
This project is ideally aimed at those who are comfortable with the DevinVision boilderplate but who wish to save time in setting it up.
git clone [email protected]:Ludwig44/WordPress-Plugin-Boilderplate-webpack-composer.git;
cd WordPress-Plugin-Boilderplate-webpack-composer;
bash install.bash
All constantes defined by the plugin (the name PLUGIN_NAME
is replace by your plugin name):
PLUGIN_NAME_VERSION
: The version of your plugin.PLUGIN_NAME_TEXT_DOMAIN
: The text domain of your plugin for internationnalisation.PLUGIN_NAME_PLUGIN_PATH
: The plugin path for include files.PLUGIN_NAME_PLUGIN_URL
: The plugin url to include script or style for example.The plugin template embed a cron job class, for easily add cron job. For add one just follow this steps:
/admin/class-cron-job
, in plugin_name_crons_list()
function'exemple' => array(
"file_path" => "admin/cron/exemple.php", // Replace by your cron job file path
'interval' => HOUR_IN_SECONDS * 24, // Replace by your interval
'display' => __( 'Exemple of description', PLUGIN_NAME_TEXT_DOMAIN ) // Replace by your description
)
/admin/cron/
or in other path./languages/plugin-name.pot
with Poedit.Execute all command for composer in /includes
. For more documentation for composer follow this link.
The vendor is already installed when your plugin is generated unless composer was not installed on your machine, in this case go to /includes
and run composer install
.
Execute all command for @wordpress/scripts in /public/assets
.
Just launch npm install
public/assets/src/js
add a file for exemple exemple.js
, you can import other files and component. For more information show this documentation.npm run dev
or npm run prod
command.public/assets/build
.public/assets/src/scss
add a file for exemple exemple.scss
.public/assets/src/js/exemple.js
an import for scss file.public/assets/build
.npm run dev
for use hot reload and non minified code for js and css.npm run prod
for minified css and js code.When you compile files, you have 3 types of files in public/assets/build
folder:
plugin-name.asset.php
: This file is for register script and style in WordPress.plugin-name.js
: This file is for js code.plugin-name.css
: This file is for css code.For use this files in WordPress, you have to register script and style in your plugin. For this you have to use wp_register_script
and wp_register_style
functions. For more information follow this link.
You can use this code for register script and style in WordPress:
$assets_data = include( PLUGIN_NAME_PLUGIN_PATH . 'public/assets/build/plugin-name.asset.php' );
wp_enqueue_script( 'plugin-name-js', PLUGIN_NAME_PLUGIN_URL . 'public/assets/build/plugin-name.js', $assets_data['dependencies'] ?? array(), $assets_data['version'] ?? filemtime( PLUGIN_NAME_PLUGIN_PATH . 'public/assets/build/plugin-name.js' ), true );
wp_enqueue_style( 'plugin-name-css', PLUGIN_NAME_PLUGIN_URL . 'public/assets/build/plugin-name.css', array(), filemtime( PLUGIN_NAME_PLUGIN_PATH . 'public/assets/build/plugin-name.css' ) );