make me a wordpress plugin that will easily let me use React
- User
make me a wordpress plugin that will easily let me use React
Assistant
Creating a WordPress plugin that lets you easily use React is a great way to bring modern JavaScript (JSX/React) into your WordPress site.
Below is a simple WordPress plugin boilerplate that:
- Enqueues a React app
- Renders it in a shortcode
[react_app] - Uses the React and ReactDOM libraries from WordPress core, so no need to install or bundle them yourself
✅ Step 1: Create Plugin Folder
In your WordPress plugins folder:
/wp-content/plugins/react-app-shortcode/Create the following files inside that folder:
1.
react-app-shortcode.php(Main plugin file)<?php /* Plugin Name: React App Shortcode Description: Easily embed a React app using a shortcode [react_app] Version: 1.0 Author: Your Name */ defined('ABSPATH') or die('No script kiddies please!'); // Register shortcode function react_app_shortcode() { ob_start(); ?> <div id="react-app"></div> <?php return ob_get_clean(); } add_shortcode('react_app', 'react_app_shortcode'); // Enqueue scripts and styles function enqueue_react_app() { // Only load on pages that have the shortcode if (has_shortcode(get_post()->post_content, 'react_app')) { wp_enqueue_script( 'react-app', plugins_url('build/index.js', __FILE__), ['react', 'react-dom'], filemtime(plugin_dir_path(__FILE__) . 'build/index.js'), true ); wp_enqueue_style( 'react-app-style', plugins_url('build/index.css', __FILE__), [], filemtime(plugin_dir_path(__FILE__) . 'build/index.css') ); } } add_action('wp_enqueue_scripts', 'enqueue_react_app');
2. Create
src/index.js(Your React code)This is where you write your React components.
import React from 'react'; import ReactDOM from 'react-dom'; const App = () => { const [count, setCount] = React.useState(0); return ( <div style={{ padding: '20px', border: '1px solid #ccc' }}> <h2>Hello from React!</h2> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); }; document.querySelectorAll('#react-app').forEach(container => { ReactDOM.render(<App />, container); });
3. Optional:
src/index.cssYou can add some basic styling for your React app.
body { font-family: sans-serif; }
✅ Step 2: Build Your React App
To make this work, we need to build our React app with a bundler like Vite, Webpack, or Parcel.
We'll use Vite here as an example because it's fast and easy.
Install Vite and dependencies (inside your plugin folder):
cd /path/to/wp-content/plugins/react-app-shortcode npm create vite@latest . --template react # When asked, say NO to TypeScript, CSS pre-processors, etc. npm installThen update the
vite.config.jsto output the build correctly:Update
vite.config.js:import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [react()], build: { outDir: 'build', emptyOutDir: true, rollupOptions: { input: { main: './src/main.jsx' } } } });If you're using the standard
index.js, renamemain.jsxtoindex.jsx.
Add a
package.jsonscript to build:"scripts": { "dev": "vite", "build": "vite build" }Now run:
npm run buildThis will generate a
/buildfolder containingindex.jsandindex.css.
✅ Step 3: Activate and Use the Plugin
- Go to WordPress Admin → Plugins
- Find React App Shortcode and activate it.
- In any post/page, insert the shortcode:
[react_app]
🧠 Notes
- This setup uses WordPress’ built-in React (
wp.element) when available (since WP 5.8+). - For advanced apps, consider using
create-react-appor Vite + custom bundling. - You can also use JSX directly by setting up Babel in your build process.
✅ Final File Structure
react-app-shortcode/ ├── react-app-shortcode.php ← Main plugin PHP file ├── package.json ← npm config ├── vite.config.js ← Vite bundler config ├── src/ │ ├── main.jsx ← React entry point │ ├── App.jsx ← Your React component │ └── index.css ← Styles └── build/ ├── index.js ← Built JS └── index.css ← Built CSS
Let me know if you'd like:
- A version using Create React App
- To pass data from PHP to React (e.g., user info)
- A block-based editor integration (Gutenberg)
Happy coding! 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.