Function and usage of block_editor_rest_api_preload() in wordpress


Answers ( 1 )


    The block_editor_rest_api_preload() function in WordPress is designed to improve the performance and user experience of the block editor (also known as Gutenberg). This function allows developers to specify an array of REST API paths to preload data for a given block editor context. Here's a detailed explanation:


    • Name: block_editor_rest_api_preload
    • Purpose: To preload common data used with the block editor by preloading specified REST API paths.
    • How it Works: The function takes in a list of REST API paths and a block editor context, and preloads the data from those paths. This preloading helps in reducing the load times and improving the responsiveness of the block editor by fetching necessary data in advance.


    1. $preload_paths (required)

      • Type: (string|string[])[]
      • Description: This is an array of paths that you want to preload. Each path should correspond to a specific REST API endpoint. These paths are used to fetch data that the block editor will likely need, reducing the need for additional network requests during the editor's operation.
    2. $block_editor_context (required)

      • Type: WP_Block_Editor_Context
      • Description: This parameter represents the current block editor context. It provides context-specific information to the preloading process, ensuring that the data being preloaded is relevant to the current state or requirements of the block editor.

    Sample Usage:

    Here's an example of how block_editor_rest_api_preload() might be used in a WordPress theme or plugin:

    function my_custom_preload_paths( $preload_paths, $block_editor_context ) {
        // Add a custom REST API path to the preload list
        $preload_paths[] = '/wp/v2/my_custom_endpoint';
        // Preload data for a specific post type, if in the post editor
        if ( isset( $block_editor_context->post ) && 'my_post_type' === $block_editor_context->post->post_type ) {
            $preload_paths[] = '/wp/v2/another_custom_endpoint';
        return $preload_paths;
    add_filter( 'block_editor_rest_api_preload_paths', 'my_custom_preload_paths', 10, 2 );

    In this example, custom REST API paths are added to the preload list. The function my_custom_preload_paths is hooked to the block_editor_rest_api_preload_paths filter, allowing it to modify the list of preloaded paths based on the current block editor context (e.g., adding specific endpoints for a custom post type editor).


    • This function is especially useful for themes and plugins that extend the block editor with custom blocks or features that rely on REST API data.
    • Proper usage can significantly enhance the block editor's performance, making it more responsive and user-friendly.
    • It's important to carefully select the paths to preload, as unnecessary preloading can lead to performance issues.

Leave an answer