A ready-to-use WordPress Plugin that makes it easy to integrate React JS into the development of a WordPress Plugin. You can create your JSX components and compile them into JavaScript, which will be enqueued by WordPress.
Install the module bundler Webpack v5+ and webpack-cli globally:
npm install -g webpack
npm install -g webpack-cli
-
Clone the repository to the Plugins directory of your WordPress installation:
/wp-content/plugins/
. -
Install the dependencies:
npm install
-
Use the provided npm scripts to build the project:
-
For development:
npm run dev
This will generate the
public/bundle.js
file with source maps for debugging. -
For production:
npm run build
This will generate the optimized and minified
dist/public/bundle.js
file. -
To watch for file changes:
npm run watch
Automatically rebuilds the development bundle when changes are detected.
-
To clean the output:
npm run clean
Removes old build files from the
public/
anddist/public/
directories.
-
The plugin creates a menu entry under Tools -> WP Plugin React
. Visit this page to see the result of your React components.
-
Container Component for Dynamic Component Management:
- Introduces a
Container
component that dynamically loads and renders multiple components in a structured layout. - Works seamlessly with the
DynamicLoader
to fetch and render components from the centralized registry.
How It Works:
- The
Container
defines a list of component names:const componentsToLoad = ["Default", "PostFetcher"];
- Each component is dynamically loaded using
DynamicLoader
.
Example: Adding a New Component
- Create your component in the
src/components/
directory:const MyNewComponent = () => <div>Hello from MyNewComponent!</div>; export default MyNewComponent;
- Register it in
src/registry/registerComponents.js
:import { registerComponent } from "./componentRegistry"; import MyNewComponent from "../components/MyNewComponent"; registerComponent("MyNewComponent", MyNewComponent);
- Add the component name to
componentsToLoad
inContainer.jsx
:const componentsToLoad = ["Default", "PostFetcher", "MyNewComponent"];
- Introduces a
-
Dynamic Component Loading:
- Allows developers to register and dynamically load React components without modifying the plugin core.
- Uses a centralized registry (
src/registry/componentRegistry.js
) to manage components. - The
DynamicLoader
component loads components by their registered names.
Example: Registering a Component
import { registerComponent } from "./src/registry/componentRegistry"; import MyComponent from "./src/components/MyComponent"; registerComponent("MyComponent", MyComponent);
Dynamic Loading Example:
<DynamicLoader componentName="MyComponent" />
-
React Context API for Global State Management:
- Provides a global state management solution using React's Context API.
- Includes a generic
AppContext
andAppProvider
for managing state across components.
Example: Using Context API in a Component
import React, { useContext } from "react"; import { AppContext } from "../context/AppContext"; const MyComponent = () => { const { state, updateState } = useContext(AppContext); return ( <div> <p>Current Value: {state.exampleKey}</p> <button onClick={() => updateState("exampleKey", "New Value")}> Update Value </button> </div> ); };
-
Dynamic Configurations:
- Supports dynamic plugin configurations using a centralized
src/config.js
file. - Merges static defaults with dynamic values provided by WordPress via
wp_localize_script
.
Usage Example:
import config from "../config"; console.log(config.apiBaseUrl); // Access dynamic configurations
- Supports dynamic plugin configurations using a centralized
-
Flexible Build System:
- Includes
dev
,build
,clean
, andwatch
npm scripts for easy development and deployment.
- Includes
If you encounter an error related to the script execution policy when running webpack
in PowerShell, the error typically looks like:
webpack: Unable to load the file C:\Users\YourUsername\AppData\Roaming\npm\webpack.ps1 because
script execution is disabled on this system.
Solution:
-
Open PowerShell as an administrator.
-
Run the following command to set the execution policy for the current user:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
- Edit your React components in the
src/components/
directory. - Register new components in
src/registry/registerComponents.js
. - Use the
Container
to dynamically render multiple components. - Use
npm run dev
during development for faster builds. - Use
npm run build
to generate a production-ready bundle. - Configure plugin settings in
src/config.js
and pass dynamic values from WordPress usingwp_localize_script
. - Visit the
Tools -> WP Plugin React
page to see your changes in action.