Using WordPress ‘block_editor_rest_api_preload_paths’ PHP filter

The block_editor_rest_api_preload_paths WordPress PHP filter allows you to modify the array of REST API paths that will be preloaded for the block editor.

Usage

add_filter( 'block_editor_rest_api_preload_paths', 'your_custom_function', 10, 2 );

function your_custom_function( $preload_paths, $block_editor_context ) {
    // your custom code here
    return $preload_paths;
}

Parameters

  • $preload_paths (string|string[])[] – Array of paths to preload.
  • $block_editor_context (WP_Block_Editor_Context) – The current block editor context.

More information

See WordPress Developer Resources: block_editor_rest_api_preload_paths

Examples

Preload additional REST API path

Preload a custom post type in the block editor.

add_filter( 'block_editor_rest_api_preload_paths', 'preload_custom_post_type', 10, 2 );

function preload_custom_post_type( $preload_paths, $block_editor_context ) {
    // Add custom post type REST API path
    $preload_paths[] = '/wp/v2/my_custom_post_type';
    return $preload_paths;
}

Remove REST API path from preloading

Remove the preloading of categories to reduce initial load time.

add_filter( 'block_editor_rest_api_preload_paths', 'remove_preload_categories', 10, 2 );

function remove_preload_categories( $preload_paths, $block_editor_context ) {
    // Remove the categories REST API path
    $preload_paths = array_diff( $preload_paths, array( '/wp/v2/categories' ) );
    return $preload_paths;
}

Conditionally preload paths based on post type

Preload different REST API paths based on the post type being edited.

add_filter( 'block_editor_rest_api_preload_paths', 'conditionally_preload_paths', 10, 2 );

function conditionally_preload_paths( $preload_paths, $block_editor_context ) {
    // Get the current post type
    $post_type = $block_editor_context->post->post_type;

    if ( 'custom_post_type' === $post_type ) {
        // Add a custom REST API path for custom post type
        $preload_paths[] = '/wp/v2/some_custom_data';
    }

    return $preload_paths;
}

Preload multiple REST API paths

Preload multiple paths at once for a better editing experience.

add_filter( 'block_editor_rest_api_preload_paths', 'preload_multiple_paths', 10, 2 );

function preload_multiple_paths( $preload_paths, $block_editor_context ) {
    // Add multiple REST API paths
    $new_paths = array( '/wp/v2/path_one', '/wp/v2/path_two', '/wp/v2/path_three' );
    $preload_paths = array_merge( $preload_paths, $new_paths );

    return $preload_paths;
}

Preload REST API path with query parameters

Preload a REST API path with specific query parameters for filtering.

add_filter( 'block_editor_rest_api_preload_paths', 'preload_path_with_parameters', 10, 2 );

function preload_path_with_parameters( $preload_paths, $block_editor_context ) {
    // Add REST API path with query parameters
    $preload_paths[] = '/wp/v2/my_custom_data?per_page=5&order=asc';

    return $preload_paths;
}