Vue Wordpress Plugin Starter with Vue3, Typescript, and Laravel Mix (Webpack wrapper)
Effortlessly create a new plugin with this template!
Preview online: https://niiknow.github.io/vue-wp-plugin-starter/
Scaffolding
As this is a template project, simply click the "Use this template" button and run:
npm run make-plugin
Use composer install
to install php dependencies, and use npm install
for front-end dependencies.
To build wp-your-plugin-name.zip
for deployment:
composer app:package
Deploy wp-your-plugin-name.zip
to a wordpress website:
unzip wp-your-plugin-name.zip
To make plugin translation, run npm run make-pot
. This will scan includes/*.php
and src/*.vue
to generate pot
files to languages/
folder. Then run npm run compile-vue-translation
to create languages/translation.json
to use with vue front-end. Again, the process is:
npm run make-pot
# use https://poedit.net/ to edit your *.po files under languages/ folder
npm run compile-vue-translation
Also, don't forget to update readme.txt file appropriately since it is one of wordpress plugin required file.
There are two front-end apps in this starter/example. Let say your prefix is wp-awesome-plugin
, then your shortcodes would be (also see include/FrontendLoader.php
):
<!-- frontend app (full route) example -->
[wp-awesome-plugin-vue-app postfix='frontend']
<!-- frontview app (dynamic view) example, render component in frontview/views/Comp2.vue folder -->
[wp-awesome-plugin-vue-app postfix='frontview' view="Comp2"]
Since admin does not require shortcode, it is automatically generate with wrapper id=admin-app-wrapper
Run/preview locally:
npm run watch
vue-wp-plugin-starter/
ββββassets/
β admin.css - postcss for wordpress admin
β admin.html - admin demo html template
β frontend.css - postcss for front-end supporting full routing
β frontend.html - front-end demo html template
β frontview.css - postcss for front-end allow dynamic vue view/page
β frontview.html - demo of dynamic vue view/page html template
ββββbin/
β makearchive.js - zip up the plugin for deployment
β makeplugin.js - init plugin
β makepot.js - make php translate file
ββββconfig/
β settings.php - plugin config file for admin settings page
ββββincludes/
ββββApi/
| β SettingsController.php - REST Api for admin settings
β AdminLoader.php - load wordpress admin stuff
β ApiRouters.php - load REST Api routes
β Assets.php - maintain plugin assets
β CliLoader.php - load CLI commands
β ExampleCommand.php - hello world command
β FrontendLoader.php - load vue front-end shortcodes
β Main.php - Main plugin instance
β Migrations.php - handle database migrations
ββββpublic/
ββββcss/ - output of assets/ folder
ββββjs/ - compiled/output of src
β mix-manifest.json - versioning builted assets
ββββsrc/
ββββadmin/
β ββββrouter/
β | | index.ts - map all route/files in admin router folder
β | admin-menu-fix.js - SPA menu selector for wordpress admin
β | admin.ts - admin main entry point
β | App.vue - admin main view
ββββfrontend/
β ββββrouter/
β | | index.ts - map all route/files in frontend router folder
β ββββviews/
β | | Home.Vue
β | App.vue - frontend main view
β | frontend.ts - frontend main entry point
ββββfrontview/
β ββββrouter/
β | | index.ts - just a dummy router
β ββββviews/
β | | Comp2.Vue - demo second view
β | | Home.Vue - demo main/first view
β | App.vue - main view
β | frontview.ts - main entry point
β shims-vue.d.ts - allow vue to use in typescript
β shims-window.d.ts - allow windows object use in typescript
ββββtests/
ββββjsunit/
β ββββadmin/
β β ββββviews/
β | | Dashboard.spec.ts - Demo jest/javascript unit testing
ββββphpunit/
β ββββApi/
β | | SettingControllerTests.php
β | AdminLoaderTests.php
β | PluginTestCase.php - TestCase base class
| bootstrap.php - loader file, act like (fake) plugin file in main folder
| class-wp-rest-controller.php - mock wordpress rest controller for testing
| jest.setup.ts
-----
.... root files can be seen above so it's not listed. Above listing is
.... to provide overview of our project structure so not all files are listed here.
-----
- Can I use SASS/SCSS intead of postCss?
- Yes. Simply uses mix.sass instead of mix.postCss in webpack.mix.js file. Since there are multiple assets (admin, frontend, frontview), you can also mix different css processors between them too.
- Why does my CSS failed to output?
- Is there syntax error in your CSS file? You can try sass instead of postCss transform. sass is better at letting you know if your CSS is having error. In my experience, postCss just fail and doesn't notify you of the error.
... more TBD
GPLv2 or later, see LICENSE file.