Below is a complete, minimal yet flexible starter-plugin that lets you drop React components into any WordPress site with:
• ZERO extra front-end libraries (it re-uses the React that already ships with WordPress)
• A build setup powered by @wordpress/scripts (Webpack + Babel ready out-of-the-box)
• A simple shortcode [react-root name="App"] to mount any React component you export.
Folder tree – what you’ll get
code
react-starter/
├─ react-starter.php <-- main plugin file (PHP)
├─ package.json <-- JS tooling
├─ src/
│ └─ index.jsx <-- entry point for all your React code
└─ build/ <-- auto-generated when you run “npm run build”
Plugin PHP (react-starter.php)
php
<?php
/**
* Plugin Name: React Starter
* Description: A tiny boilerplate that lets you embed React components anywhere via shortcode or template tag.
* Author: Your Name
* Version: 1.0.0
* License: GPL-2.0+
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'REACT_STARTER_PLUGIN_VERSION', '1.0.0' );
define( 'REACT_STARTER_PLUGIN_DIR', plugin_dir_url( __FILE__ ) );
/**
* Register & enqueue the compiled JS.
*/
function rs_enqueue_react_app() {
// Path to the compiled file created by `npm run build` (see package.json)
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
wp_register_script(
'react-starter-app',
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'], // Normally => ['wp-element', …]
$asset_file['version'], // Cache-busting
true // In footer
);
// An example of localised data (optional)
wp_localize_script( 'react-starter-app', 'RS_DATA', [
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'siteName'=> get_bloginfo( 'name' ),
] );
}
add_action( 'wp_enqueue_scripts', 'rs_enqueue_react_app' ); // front-end
add_action( 'admin_enqueue_scripts', 'rs_enqueue_react_app' );// wp-admin
/**
* Shortcode: [react-root name="App" class="my-wrapper"]
* “name” must match the component you export from src/index.jsx
*/
function rs_react_root_shortcode( $atts ) {
$atts = shortcode_atts( [
'name' => 'App',
'class' => '',
'id' => '',
], $atts, 'react-root' );
$id = $atts['id'] ? esc_attr( $atts['id'] ) : 'react-root-' . wp_generate_uuid4();
$class = esc_attr( $atts['class'] );
$name = esc_attr( $atts['name'] );
// Ensure script is queued exactly once
wp_enqueue_script( 'react-starter-app' );
// Pass component name & root id via data attributes
return sprintf(
'<div id="%s" class="%s" data-rs-component="%s"></div>',
$id,
$class,
$name
);
}
add_shortcode( 'react-root', 'rs_react_root_shortcode' );
React / JS side (src/index.jsx)
jsx
/**
* Entry point for all components.
* WordPress provides React through the global "wp.element" package.
*/
import { createRoot } from 'react-dom/client';
import { createElement, useState } from '@wordpress/element';
/* -----------------------------------------------------------------------
1) Define / export any components you like
----------------------------------------------------------------------- */
export function App() {
const [count, setCount] = useState(0);
return (
<div style={{padding: 20, textAlign: 'center', color: '#fff'}}>
<h2>Hello from React Starter 👋</h2>
<p>Site title from PHP: <strong>{RS_DATA.siteName}</strong></p>
<button onClick={() => setCount(c => c + 1)}>Clicks: {count}</button>
</div>
);
}
/* More components? Just export them: */
export function FancyWidget() {
return <em>I'm another component</em>;
}
/* -----------------------------------------------------------------------
2) Auto-mount any <div> added by the shortcode/template tag above
----------------------------------------------------------------------- */
document.addEventListener('DOMContentLoaded', () => {
// Query all root placeholders added by PHP
const roots = document.querySelectorAll('[data-rs-component]');
roots.forEach(node => {
const componentName = node.dataset.rsComponent;
// Dynamically pick the right exported component
const Component = exports[componentName];
if (Component) {
createRoot(node).render(createElement(Component));
} else {
console.error(`React Starter: component “${componentName}” not found.`);
}
});
});
Edit src/index.jsx, run npm start, and watch your code live-reload 💥
Why this approach?
• Uses wp-element (WordPress’ built-in React + ReactDOM) so visitors download one copy of React, not two.
• Asset manifest (build/index.asset.php) guarantees WP loads the right deps + version every build.
• Shortcode/template tag keeps PHP/JS loosely coupled—you can have multiple independent React roots on one page.
• The build toolchain is the same Gutenberg core uses, so future-proof and low-friction.