React Plugin
A WordPress plugin using React.js.
Install
This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.
Install straight from GitHub with WP-CLI:
wp plugin install https://github.com/abdurraahimm/wordpress-react-plugin/archive/refs/heads/main.zipReact Plugin for WordPress
This is a simple example of how to create a React plugin for WordPress using Vite.
Installation
- Clone the repository or download the zip file.
- Extract the files to your WordPress plugins directory.
- Activate the plugin in your WordPress admin dashboard.
- Add the
[react_app]shortcode to a page or post to see the React app.
Starter Files
Go to starter branch to get the starter files. This branch contains the basic setup for the plugin.
Notes
Development Mode:Usenpx vitefor development with hot reloading. Ensure the URL matches your WordPress local development server.Production Build:Always usenpx vite buildbefore deploying your plugin.Dynamic WordPress Data:Usewp_localize_script()in PHP to pass dynamic data from WordPress to React.
Manual Setup
Step 1: Set Up the Plugin Directory
Create the directory for your plugin in the WordPress plugins folder:
cd /path/to/wp-content/plugins
mkdir react-plugin
cd react-plugin
Step 2: Initialize the Project
Run the following commands to initialize the project and install dependencies:
npm init -y
npm install react react-dom --save
npm install vite @vitejs/plugin-react --save-dev
Step 3: Write the Main Plugin File
Create the main PHP file react-plugin.php to register the plugin and enqueue the React app.
react-plugin.php
<?php
/**
* Plugin Name: React Plugin
* Description: A WordPress plugin using React.js.
* Version: 1.0
* Author: Your Name
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
// Enqueue the React app script.
function react_plugin_enqueue_assets() {
// Enqueue the compiled React app.
wp_enqueue_script(
'react-app',
plugin_dir_url(__FILE__) . 'dist/assets/main.js',
[],
null,
true
);
}
add_action('wp_enqueue_scripts', 'react_plugin_enqueue_assets');
// Add a shortcode to display the React app.
function react_plugin_shortcode() {
return '<div id="react-app"></div>';
}
add_shortcode('react_app', 'react_plugin_shortcode');
Step 4: Set Up Vite Configuration
Create a vite.config.js file in the root directory to configure Vite for React:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
input: path.resolve(__dirname, 'src/main.jsx'), // Set entry point
output: {
entryFileNames: 'assets/[name].js',
},
},
outDir: 'dist', // Output directory
emptyOutDir: true,
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
});
Step 5: Write the Entry Point
Create the entry point for your React app in the src/main.jsx file:
// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import HelloWorld from './components/HelloWorld';
const App = () => {
return (
<div>
<HelloWorld />
</div>
);
};
const rootElement = document.getElementById('react-app');
if (rootElement) {
ReactDOM.createRoot(rootElement).render(<App />);
}
Step 6: Create React Components
Create React components in the src/components directory.
Step 7: Install Dev Dependencies
Install @babel/preset-react to handle JSX during the build process:
npm install @babel/preset-react --save-dev
Step 8: Build the Plugin
Run the following command to build the plugin:
npx vite build
This will generate the dist/ folder containing the compiled JavaScript assets.
Step 9: Activate the Plugin
Go to your WordPress Admin Dashboard. Navigate to Plugins → Installed Plugins. Activate the React Plugin.
Step 10: Add the Shortcode
Add the [react_app] shortcode to a page or post in WordPress. When you view the page, your React app should be rendered.
Final File Structure
react-plugin/
├── react-plugin.php
├── package.json
├── vite.config.js
├── src/
│ ├── main.jsx
│ ├── components/
│ │ └── HelloWorld.jsx
├── dist/
│ ├── assets/
│ └── app.js