Learn to register a custom data store in WordPress using @wordpress/data. Master createReduxStore and store registration to centralize your plugin's state.
Previously in this course, we explored the Understanding WordPress Data Store Architecture | @wordpress/data Guide to grasp the theoretical foundations of the store/selector/action pattern. In this lesson, we move from theory to implementation by creating and registering a custom data store for our Knowledge Base plugin.
By centralizing our state, we avoid "prop drilling" and ensure that multiple components can access the same source of truth for our Knowledge Base articles.
In WordPress, state management is handled by a global registry provided by the @wordpress/data package. To add our own data to this ecosystem, we use createReduxStore, which follows the standard Redux pattern but is optimized for the WordPress environment.
Registering a store involves three primary steps:
createReduxStore to generate the store instance.register method of the wp.data registry.Let's create a new file, src/store/index.js, to house our store configuration. For now, we will focus on defining the initial state and performing the registration.
JAVASCRIPT/** * External dependencies */ import { createReduxStore, register } from CE9178">'@wordpress/data'; /** * Initial State */ const DEFAULT_STATE = { items: [], isFetching: false, error: null, }; /** * Store Configuration */ const storeConfig = { reducer(state = DEFAULT_STATE, action) { // We will fill this in next lesson return state; }, selectors: { getItems(state) { return state.items; } }, actions: { setItems(items) { return { type: CE9178">'SET_ITEMS', items }; } } }; /** * Create and register the store */ const store = createReduxStore(CE9178">'kb/knowledge-base', storeConfig); register(store);
Once the store is registered, it becomes available globally in the WordPress admin. You must ensure this file is imported in your main entry point (e.g., src/index.js) so that the code executes when the page loads.
Simply adding import './store'; at the top of your main entry point ensures that the registration logic runs before your React components attempt to access the store via useSelect or useDispatch.
src/store directory in your plugin folder.src/store/index.js and implement the createReduxStore call using the namespace kb/knowledge-base.src/index.js entry point.wp.data.select('kb/knowledge-base').getItems(). Verify that it returns the empty array from your DEFAULT_STATE.kb/). Using a generic name like data will conflict with core WordPress stores and cause unpredictable behavior.undefined. Always import your store file at the very top of your application entry point.@wordpress/data is a singleton registry. You don't need to pass the store instance down through props; once registered, any component in your React tree can access it using the standard hooks.We have successfully moved beyond local component state—which we previously managed using techniques like those described in Handling Asynchronous State in React for WordPress Plugins—into a formal, global state management pattern. By using createReduxStore and register, we have created a scalable foundation for the Knowledge Base plugin that allows for complex data interactions across the entire admin dashboard.
Up next: We will implement the logic to query this store using custom selectors and memoization.
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 selectors in @wordpress/data to efficiently retrieve state. Learn to implement memoization for high-performance React admin interfaces in WordPress.
Registering a Custom Data Store
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