Using WordPress ‘block_editor_preload_paths’ PHP filter

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

Usage

add_filter('block_editor_preload_paths', 'your_custom_function_name', 10, 2);

function your_custom_function_name($preload_paths, $selected_post) {
    // your custom code here
    return $preload_paths;
}

Parameters

  • $preload_paths (string|string[][]): An array of paths to preload.
  • $selected_post (WP_Post): The post being edited.

More information

See WordPress Developer Resources: block_editor_preload_paths

Examples

Preloading a custom path

Preload a custom REST API path for your plugin or theme.

add_filter('block_editor_preload_paths', 'preload_custom_path', 10, 2);

function preload_custom_path($preload_paths, $selected_post) {
    $preload_paths[] = '/your-plugin/v1/some-data';
    return $preload_paths;
}

Adding paths for specific post types

Preload paths only for a specific post type.

add_filter('block_editor_preload_paths', 'preload_paths_for_post_type', 10, 2);

function preload_paths_for_post_type($preload_paths, $selected_post) {
    if ($selected_post->post_type === 'your_post_type') {
        $preload_paths[] = '/your-plugin/v1/some-data';
    }
    return $preload_paths;
}

Removing a path from the preload list

Remove a path from the default preload list.

add_filter('block_editor_preload_paths', 'remove_path_from_preload', 10, 2);

function remove_path_from_preload($preload_paths, $selected_post) {
    $index = array_search('/wp/v2/types', $preload_paths, true);
    if (false !== $index) {
        unset($preload_paths[$index]);
    }
    return $preload_paths;
}

Preload custom paths for specific user roles

Preload paths only for users with a specific role.

add_filter('block_editor_preload_paths', 'preload_paths_for_user_role', 10, 2);

function preload_paths_for_user_role($preload_paths, $selected_post) {
    $current_user = wp_get_current_user();
    if (in_array('your_role', $current_user->roles, true)) {
        $preload_paths[] = '/your-plugin/v1/some-data';
    }
    return $preload_paths;
}

Preload paths based on post meta

Preload paths only if a specific post meta value is set.

add_filter('block_editor_preload_paths', 'preload_paths_based_on_meta', 10, 2);

function preload_paths_based_on_meta($preload_paths, $selected_post) {
    $post_meta = get_post_meta($selected_post->ID, 'your_meta_key', true);
    if ('your_meta_value' === $post_meta) {
        $preload_paths[] = '/your-plugin/v1/some-data';
    }
    return $preload_paths;
}