Learn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.
Previously in this course, we built a robust service layer in Building the Knowledge Base Service Layer to handle our API interactions. Now that our data fetching is ready, we need a place to display it. In this lesson, we are scaffolding the React admin dashboard, which involves three distinct phases: registering the WordPress admin menu, creating a React container component, and finally, mounting that component into the DOM.
WordPress is a server-side framework that renders HTML via PHP. React, however, is a client-side library that manages its own virtual DOM. To bring them together, we use a "Mounting Pattern."
We output an empty <div> with a unique ID from PHP. Then, we write a small JavaScript snippet that waits for the DOM to be ready, finds that <div>, and tells React to "take over" that specific node. This keeps WordPress in charge of the page structure while letting React handle the dynamic UI components.
First, we need a persistent home for our dashboard. Open your main plugin file and register a new top-level menu page.
PHPadd_action( 'admin_menu', 'kb_register_admin_page' ); function kb_register_admin_page() { add_menu_page( 'Knowledge Base Dashboard', 'Knowledge Base', 'manage_options', 'knowledge-base', 'kb_render_admin_page', 'dashicons-book-alt' ); } function kb_render_admin_page() { #6A9955">// This div is the "root" where React will mount echo '<div id="kb-admin-app"></div>'; }
In your src/ directory, create a folder named components and a file inside called App.js. This will serve as the root of your application.
JSXimport React from CE9178">'react'; const App = () => { return ( <div className="kb-container"> <h1>Knowledge Base Dashboard</h1> <p>React is successfully mounted!</p> </div> ); }; export default App;
Now, we need to wire these two pieces together. Create an index.js file in your src/ directory. This is the entry point that your build process (configured in Introduction to @wordpress/scripts) will compile into a standalone file.
JAVASCRIPTimport React from CE9178">'react'; import { createRoot } from CE9178">'react-dom/client'; import App from CE9178">'./components/App'; const container = document.getElementById(CE9178">'kb-admin-app'); if (container) { const root = createRoot(container); root.render(<App />); }
After creating these files, remember to run npm run build in your terminal. This command processes your JSX and creates the compiled JavaScript file in your build/ folder. Ensure you are enqueuing this file in your plugin using wp_enqueue_script, making sure to list wp-element as a dependency so that React is available in the browser.
wp_enqueue_script call to your admin_menu hook, pointing to your compiled build/index.js.App.js rendered on the admin page, you have successfully scaffolded your dashboard.App.js using useState that toggles a "Loading..." message when the component mounts.wp-element dependency: If you forget to add wp-element to your script dependencies, React will be undefined, and your code will throw a silent error. Always check the network tab to see if react and react-dom are loading.document.getElementById matches exactly what you echoed in your PHP function.defer attribute or enqueuing your script in the footer ($in_footer = true) is best practice.#kb-admin-app div.We've moved from static PHP pages to a dynamic React-powered dashboard. By registering a dedicated menu, creating a root component, and mounting it into a specific DOM node, we've created the architecture needed for a modern WordPress plugin. This setup is the foundation for all the interactive features we'll build in the following lessons.
Up next: We will begin populating this dashboard with actual UI elements by working with @wordpress/components.
Stop scattering fetch calls across your React components. Learn to build a clean service layer to centralize API logic and simplify your WordPress plugin.
Read moreMaster React state management for asynchronous API requests. Learn to implement loading, error, and success states to create a seamless WordPress admin UI.
Scaffolding the React Admin Dashboard
Understanding WordPress Data Store Architecture
Registering a Custom Data Store
Writing Selectors for Data Access
Defining Actions and Reducers
Implementing Resolvers for Data Fetching
Optimizing Performance with Selectors
Handling Complex State Dependencies
Implementing Nonce Verification
Advanced Sanitization Techniques
Input Validation and Error Handling
Protecting Admin Screens
Production Build Pipeline
Debugging React in the WordPress Admin
Building Search and Filter Functionality
Internationalization in React
Managing File Uploads via REST API
Optimizing API Response Times
Working with Date and Time in React
Implementing Drag-and-Drop Sorting
Creating Custom Hooks for API Logic
Integrating with Gutenberg Blocks
Handling Conflict Resolution
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web