Master the @wordpress/data architecture. Learn how the store, selector, and action pattern provides a robust, global state for your WordPress plugins.
Previously in this course, we explored Handling Asynchronous State in React for WordPress Plugins, where we managed local loading and error states within individual components using useState. While useState is excellent for isolated UI logic, it falls short when multiple components need to share data or when your application grows in complexity.
In this lesson, we move beyond local state and into the @wordpress/data architecture. This is the same Redux-based system that powers the WordPress block editor, providing a predictable, global state layer for your plugins.
As your plugin expands, you'll find yourself "prop drilling"—passing data down through five layers of components just to reach a child that needs it. You might also notice that when you update a piece of data in one part of your dashboard, other parts stay stale because they don't share a single source of truth.
In a standard React app, you might reach for the Context API or Redux. In WordPress, we use @wordpress/data, which provides a standardized way to implement a centralized state. It solves three critical problems:
The WordPress data architecture relies on a "Store," which acts as the single source of truth. To interact with this store, we follow a strict unidirectional flow:
The store holds your application's state. It is a structured object where your data lives. In our Knowledge Base project, the store will hold our collection of articles.
Selectors are functions used to retrieve data from the store. Instead of accessing state directly, you call a selector.
Example: select('my-plugin/kb').getArticles().
Crucially, selectors are often memoized. If the underlying data hasn't changed, the selector returns the cached result, saving precious CPU cycles.
Actions are plain JavaScript objects that describe what happened (e.g., ADD_ARTICLE). You "dispatch" an action to the store. A "Reducer" then listens for that action and updates the state immutably.
Resolvers are a unique WordPress feature. They are functions that automatically trigger API requests when a selector is called but the data isn't in the store yet. This keeps your components clean; they just ask for data, and the store handles the network orchestration.
You are likely already using the WordPress data layer without realizing it. When you interact with the block editor, you are querying the core data stores.
JAVASCRIPTimport { useSelect } from CE9178">'@wordpress/data'; const MyComponent = () => { // We use useSelect to hook into the global store const user = useSelect((select) => { return select(CE9178">'core').getCurrentUser(); }, []); if (!user) return <p>Loading...</p>; return <h1>Welcome back, {user.name}</h1>; };
In this example, useSelect subscribes the component to the core data store. When the user object changes, the component automatically updates. If the user data wasn't already cached, the core store's internal resolver would have fetched it from the REST API automatically.
To understand how this works in your own environment, open your browser’s console on your WordPress admin dashboard (where your plugin is active) and try to inspect the data registry:
wp.data.select('core').getAuthors() and hit enter.wp.data.dispatch('core').saveEntityRecord('postType', 'post', { title: 'Test' }).You are now interacting with the same architecture that powers the Gutenberg editor.
state.articles.push(item)). Always use actions and reducers to return a new state object. This ensures React's change detection triggers correctly.articles, create a selector that returns only articles. If you select the whole state, your component will re-render whenever any part of the state changes, causing performance bottlenecks.apiFetch inside your components if a resolver can handle it. The store’s job is to manage the lifecycle of the data; let it do its job.The @wordpress/data architecture provides a robust, scalable way to manage state in your WordPress admin screens. By mastering the store, selector, and action pattern, you ensure your plugin behaves predictably, performs efficiently, and integrates seamlessly with the rest of the WordPress ecosystem.
Next, we will begin the process of formalizing our own data layer by registering a custom store for our Knowledge Base plugin.
Up next: Registering a Custom Data Store
Learn how to implement resolvers in @wordpress/data to automate API data fetching. Discover how to sync your React state with the WordPress REST API seamlessly.
Read moreMaster the WordPress Data layer by implementing formal Actions and Reducers. Learn to manage state mutation predictably in your React-based admin dashboard.
Understanding WordPress Data Store Architecture
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