Our goal here is to make plugin development easier with React. This structure stems from our WordPress Starter Plugin Vue, but has been reworked for React.
Explore the docs ยป
View Demo ยท Report Bug ยท Request Feature
Table of Contents
WordPress plugin development is a cumbersome endeavor. This is much harder to manage when your background is Node.js and JavaScript, instead of Laravel and PHP. This boilerplate project will rapidly get you up and running with developing a new plugin.
In the spirit of a modern upgrade, we have built this into a TypeScript React plugin template.
All plugin files live inside of the /src folder. These are the instructions on setting up your project locally. To get a local copy up and running follow these simple steps.
For all of the awesome people using Node Version Manager (NVM) instead of Node.js, we have an .nvmrc file in the repo. For everyone else, please check this file to make sure that your Node.js version matches.
- Switch to correct Node.js Version
nvm useBefore you start working, you will need to rename all of the correct files, functions, assets, etc. BEFORE you run npm install. This setp is also about naming your plugin with your own namespacing.
- Manually rename any files in the entire repo that are prefixed with
wp-react-starter. - Search and replace the entire repo for any occurances of
wp-react-starter. - Search and replace the entire repo for any occurances of
wp_react_starter. - Search and replace the entire repo for any occurances of
WPReactStarter.
There are a few instances where you may not need the starting files and folder structure. In these instances, it's a good practice to delete them. Please see a few examples below.
-
i18n Languages:
/src/languages- If you have no intentions to add multiple languages, also known as "internationalization" (or i18n for short), then you can delete this folder.
-
Public Front-End:
/src/php/public- If the plugin you are developing doesn't have a need for front-end support, and is only used in the WP Admin area, then you can delete this folder.
-
TypeScript Types:
/src/utils/types- If you don't end up adding to or updating this folder, then you can remove it.
-
TypeScript Enumerations:
/src/utils/enums- If you don't end up adding to or updating this folder, then you can remove it.
-
TypeScript Interfaces:
/src/utils/interfaces- If you don't end up adding to or updating this folder, then you can remove it.
-
Vue Components:
/src/components- If you don't end up adding to or updating this folder, then you can remove it.
- Clone the repo.
git clone https://github.com/jared-leddy/wordpress-plugin-starter-react.git
- Install NPM packages.
npm install
- Run the start command to watch and build files.
npm run start:devBelow, you will find our common commands and notes for general usage.
-
Run
npm run build:dev.- When you build in dev, the plugin folder IS NOT ZIPPED. This is for those situations where you're working with local instance of WordPress using XAMP, LAMP, MAMP, etc. or even the LocalWP tool (which we use). Those steps are:
- Build the new plugin folder.
- Delete the current folder in your WP website.
- Copy your new plugin folder into the website's plugins folder.
- When you build in dev, the plugin folder IS NOT ZIPPED. This is for those situations where you're working with local instance of WordPress using XAMP, LAMP, MAMP, etc. or even the LocalWP tool (which we use). Those steps are:
-
Run
npm run build:prod.- When you build in prod, the plugin folder IS ZIPPED and ready for upload to a WP website.
-
Run
npm run start:dev.- This is runs the default
vitecommand. The terminal will tell you to open the browser to alocalhostport. We do not use the browser. - A custom hot reload plugin is located in the Vite config file to watch all files in the
/srcfolder. - Each time a file is changed, the hot reload will trigger a
npm run build:dev.
- This is runs the default
-
The
admin/index.phpis the base PHP template for the plugin settings admin page UI. It has an element<div id="app"></div>where the Vue app is getting mounted. -
The provided Vue template can be found in
src/vue/App.vue.
Vite will convert all of your .ts to .js and all .scss to .css. THey will all be placed in a /dist folder.
This covers the series of tasks that are used to initially build the plugin folder. Found in both build:dev and build:prod scripts.
-
Verify Folders Exist
- Here, we are checking to see if you have manually created your plugin folder. If you have not already created your plugin folder, we will create one for you.
- This folder needs to be present for subsequent tasks.
-
Copy Files to Folder
- Here, we are copying your entire
/srcfolder contents into your plugin folder.
- Here, we are copying your entire
-
Move Assets from Dist
- Vite is run before Gulp. All of your
.jsand.cssfiles will be located there. - For this task, those files built into the
/distfolder get moved into your plugin folder.
- Vite is run before Gulp. All of your
This covers the series of tasks that are used to clean up the plugin folder. Found in both build:dev and build:prod scripts.
- Delete Trivial Files
- Here, we delete all of the trivial files.
A trivial file typically refers to a file that contains very little or no meaningful content. These files often have minimal or placeholder code, which serves little purpose in the context of a larger project. In the context of software builds and packaging, trivial files are usually considered unnecessary and can be removed to optimize the final package.
Here are some common examples of trivial files:
- Empty files: Files that have no content at all.
- Export placeholder files: Files that contain only trivial exports like export {};, often created to satisfy module systems but don't serve any real purpose.
- Files with a single constant or trivial content: For example, a file that only defines a single constant but is never used meaningfully. See examples below:
Empty File:
// This is a completely empty fileExport Placeholder:
export {};Trivial Content:
const someVar = 'value'; // A single trivial statement with no broader impactRemoving these files can help reduce clutter, decrease the size of your final builds, and improve overall project performance.
-
Delete TypeScript Files
- Delete all files with a
.tsextension from the plugin folder . We have already used Vite to build these.jsfiles, so we don't need to retain them.
- Delete all files with a
-
Delete SCSS Files
- Delete all files with a
.scssextension from the plugin folder . We have already used Vite to build these.cssfiles, so we don't need to retain them.
- Delete all files with a
-
Clean PHP Folder
- The majority of our PHP is located in
/src/php. - During the move step above, we are moving this
phpfolder into out plugin folder.- Example:
/wp-react-starter/php
- Example:
- Here in this task, we will move all of those contents up a tree level (i.e., to
/wp-react-starter). - This step will leave an empty
phpfolder intentionally.
- The majority of our PHP is located in
-
Delete Empty Folders
- Deleting files out of our plugin folder, and moving the PHP files up a level, will leave empty folders lying around.
- Here, we will recursively delete all empty folders from the parent plugin folder (i.e.,
/wp-react-starter).
This covers the series of tasks that are used to clean up the plugin folder after the zip file is created. Found in both build:dev and build:prod scripts.
-
Delete Folders
- Delete the
/distfolder that is generated by Vite. - Delete the plugin folder (i.e.,
/wp-react-starter).
- Delete the
For this one, we will "compress" or "zip" the plugin folder. This is the step that will produce the final plugin zip file that you upload into the WordPress website. Since this one is just a task, we don't have a need to do anything more than zip the folder.
Found in build:prod script.
As we go through and develop our own plugins using these frameworks, we will document what UI frameworks are working with this starter template.
-
- We tried using this in an admin-only plugin, but had issues trying to keep Vue loaded. There is an open issue for extensive testing.
-
- We tried using this in an admin-only plugin, but ended up having to use
!importantin all of our styles. That's not the best use of a framework like this. There is an open issue for extensive testing.
- We tried using this in an admin-only plugin, but ended up having to use
We don't have a dedicated roadmap outside of Github. Simply check the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE.md for more information.
Project Link: https://github.com/jared-leddy/wordpress-plugin-starter-react
Without these people and tools, life would be too complicated.
- Good food.
- Steak ๐ฅฉ
- Ramen ๐
- Rice ๐
- Tacos ๐ฎ
- Breakfast Burritos ๐ฏ
- Burgers ๐
- Pizza ๐
- Coffee โ
- Good company.
- Family ๐ซ ๐จโ๐ผ
- Friends ๐จโ๐ซ ๐จโ๐ ๐๏ธโโ๏ธ โน๏ธโโ๏ธ ๐โโ๏ธ ๐๏ธโโ๏ธ ๐ ๐คบ ๐งโโ๏ธ ๐โโ๏ธ ๐ฃ
- Good tools.
