It needs only 10 sec to create a WP plugin with a fresh Vue + Vite setup.
Every time we create a new WordPress plugin we have to set up all the things repeatedly. Which is really time-consuming and an extra burden for us. There are some common things every WordPress plugin has. So if we can make those listed and make it reusable for the future that saves a lot of time for us. Also, we don’t have to plan or think about plugin architecture all the time for new ones. This is the WordPress boilerplate plugin which did that perfectly.
Also, HMR(hot module replacement) will save time during development and compress package size on production.
Let’s make a new plugin. Trust me it only need’s couple of seconds to make a WordPress plugin with a single-page application using Vue js and Vite.
There is no extra dependency at all. Just need the basic things to run a single-page JS application. Check that Node and Npm are installed globally on your device. You may check the installation status of the node by
node v and
npm -v for Npm.
How to create? Quick setup:
Step 1. Just clone this repository into your WordPress plugin directory (
Step 2. Open in the terminal and install npm using the command
Step 3. Call the Aladin 🧞 by command
node aladin and it will prompt your Plugin Name. Just Write the name and hit enter.
Step 4. To run the dev environment run `npm run dev`
Use the plugin
Everything is done. Check the Plugin repository to find your chosen name and activate your plugin.
Check that all of your ClassNames and slugs are updated with your new plugin info.
You may check the package.json directory to get the dependency of your application. Keep those you want or feel free to remove them. The
It has a prebuilt tailwind setup, Use it or remove it easily by deleting it from
tailwind.config.js and CSS from the
You may use the popular UI plugin element-plus as well.
Here the directory
includes contains all the PHP files you need in your plugin. You may update or modify the structure as per your needs.
plugin-entry file is the main plugin file, which loads all the classes.
/src directory contains all the vue and js files.
NB: You may not be concerned about other files/directories. They are just development dependencies. You don’t need that for production build as well.
Development Helper Class
There is an HMR setup in Vite. So you don’t have to reload all the pages when the development server is running. It will automatically load and update on DOM. However, the assets only build on production and the development assets server after the local port. So we have to change the asset’s URL when we enqueue the assets like scripts or styles.
To make that easy and more efficient it has a Vite class. we don’t need to call
Vite::enqueueScript to load scripts and
Vite::enqueueStyle for styles. All the parameters are the same as the WordPress enqueue script and enqueue style methods.
Vite::enqueueScript($enqueueTag, $yourAdminSourcePath, $dependency = , $version = null, $inFooter = false);
Vite::enqueueScript('my-plugin-script-boot', 'admin/start.js', array('jquery'), PLUGIN_CONST_VERSION, true);
$yourAdminSourcePath will be the src directory path.
You may use different ports for your site project. The default port is set to 8880. To update the port change the port declaration on includes/classes/Vite and vite.config.js file.
You only need to run
npm run production delete all except these files/directories.
- plugin-entry.php (plugin Entry file
Make them zip and upload them on any WordPress plugin directory.
Check the plugin repo link (https://github.com/hasanuzzamanbe/wp-boilerplate-vue-with-vite) for more. Or Create an issue for any dependency you face.