WordPress.org

@wordpress/core-data Edit

Core Data is a data module intended to simplify access to and manipulation of core WordPress entities. It registers its own store and provides a number of selectors which resolve data from the WordPress REST API automatically, along with dispatching action creators to manipulate data.

Used in combination with features of the data module such as subscribe or higher-order components, it enables a developer to easily add data into the logic and display of their plugin.

Installation Installation

Install the module

npm install @wordpress/core-data --save

This package assumes that your code will run in an ES2015+ environment. If you’re using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.

Top ↑

Example Example

Below is an example of a component which simply renders a list of authors:

const { withSelect } = wp.data;

function MyAuthorsListBase( { authors } ) {
    return (
        <ul>
            { authors.map( ( author ) => (
                <li key={ author.id }>{ author.name }</li>
            ) ) }
        </ul>
    );
}

const MyAuthorsList = withSelect( ( select ) => ( {
    authors: select( 'core' ).getAuthors(),
} ) )( MyAuthorsListBase );

Top ↑

Actions Actions

The following set of dispatching action creators are available on the object returned by wp.data.dispatch( 'core' ):

Refer to actions.js for the full set of dispatching action creators. In the future, this documentation will be automatically generated to detail all available dispatching action creators.

Top ↑

Selectors Selectors

The following selectors are available on the object returned by wp.data.select( 'core' ):

Refer to selectors.js for the full set of selectors. In the future, this documentation will be automatically generated to detail all available selectors.

Code is Poetry.

Skip to toolbar