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.jsfile with source maps for debugging. -
For production:
npm run build
This will generate the optimized and minified
dist/public/bundle.jsfile. -
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
Containercomponent that dynamically loads and renders multiple components in a structured layout. - Works seamlessly with the
DynamicLoaderto fetch and render components from the centralized registry.
How It Works:
- The
Containerdefines 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
componentsToLoadinContainer.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
DynamicLoadercomponent 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
AppContextandAppProviderfor 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.jsfile. - 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, andwatchnpm 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
Containerto dynamically render multiple components. - Use
npm run devduring development for faster builds. - Use
npm run buildto generate a production-ready bundle. - Configure plugin settings in
src/config.jsand pass dynamic values from WordPress usingwp_localize_script. - Visit the
Tools -> WP Plugin Reactpage to see your changes in action.
