Master CRUD operations in your WordPress admin dashboard. Learn to trigger API requests from React forms and lists to build truly interactive plugins.
Previously in this course, we covered Building the Knowledge Base Service Layer | WordPress REST API to centralize our fetch logic. In this lesson, we move from passive data fetching to active data manipulation, connecting our React components to that service layer to perform full CRUD operations.
In a React-based WordPress admin, "CRUD" isn't just about sending a request; it's about the feedback loop. When a user submits a form or deletes an item, the application must:
If you skip step 3, your UI becomes "stale," leading to user confusion when items they just deleted still appear on the screen.
We’ll use our KnowledgeBaseService to handle the heavy lifting. In your list component, you'll want to map over your data array and attach a handler to a Button component from Working with @wordpress/components for WordPress Admin UIs.
Here is how you handle a deletion operation, ensuring the UI refreshes once the server confirms the action.
JAVASCRIPTimport { useState } from CE9178">'react'; import { Button } from CE9178">'@wordpress/components'; import { deleteArticle } from CE9178">'./services/api'; // From our service layer const KnowledgeBaseList = ({ items, setItems }) => { const [isDeleting, setIsDeleting] = useState(false); const handleDelete = async (id) => { setIsDeleting(true); try { await deleteArticle(id); // Refresh UI by filtering out the deleted item setItems(items.filter(item => item.id !== id)); } catch (error) { console.error("Deletion failed:", error); alert("Failed to delete article."); } finally { setIsDeleting(false); } }; return ( <ul> {items.map(item => ( <li key={item.id}> {item.title} <Button isDestructive onClick={() => handleDelete(item.id)} disabled={isDeleting} > Delete </Button> </li> ))} </ul> ); };
The most common mistake is failing to link the API response to the component state. In the example above, setItems(items.filter(...)) is the critical step. Because we are managing state locally, we must manually reconcile the UI with the database change.
For Create operations (POST), the flow is similar:
createArticle(data).setItems([...items, newArticle]).KnowledgeBaseList component.handleDelete function that calls your deleteArticle service method.Button component is disabled while the request is in flight to prevent multiple accidental clicks.items state using the functional update pattern to ensure you are working with the latest data.isDeleting or isSubmitting state.api-fetch setup is correctly configured as discussed in Localizing Data for JavaScript in WordPress Plugins.try/catch blocks. If the server returns a 403 (Permission Denied) or 500 (Internal Server Error), the user needs to know, and the application shouldn't just "hang."We’ve successfully connected our React frontend to the backend API, allowing for real-time CRUD operations. By managing local state alongside our service layer calls, we ensure that our plugin remains performant and predictable. Remember: the API is the source of truth, but the React state is the source of the user's experience.
Up next: We will begin transitioning away from local component state to a more robust global architecture in Understanding WordPress Data Store Architecture.
Learn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.
Read moreStop scattering fetch calls across your React components. Learn to build a clean service layer to centralize API logic and simplify your WordPress plugin.
Implementing CRUD in the Admin UI
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