block_editor_rest_api_preload

The timeline below displays how wordpress function block_editor_rest_api_preload has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Preloads common data used with the block editor by specifying an array of
 * REST API paths that will be preloaded for a given block editor context.
 *
 * @since 5.8.0
 *
 * @global WP_Post    $post       Global post object.
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
 *
 * @param (string|string[])[]     $preload_paths        List of paths to preload.
 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 */
function block_editor_rest_api_preload(array $preload_paths, $block_editor_context)
{
    global $post, $wp_scripts, $wp_styles;
    /**
     * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
     *
     * @since 5.8.0
     *
     * @param (string|string[])[]     $preload_paths        Array of paths to preload.
     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
     */
    $preload_paths = apply_filters('block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context);
    if (!empty($block_editor_context->post)) {
        $selected_post = $block_editor_context->post;
        /**
         * Filters the array of paths that will be preloaded.
         *
         * Preload common data by specifying an array of REST API paths that will be preloaded.
         *
         * @since 5.0.0
         * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
         *
         * @param (string|string[])[] $preload_paths Array of paths to preload.
         * @param WP_Post             $selected_post Post being edited.
         */
        $preload_paths = apply_filters_deprecated('block_editor_preload_paths', array($preload_paths, $selected_post), '5.8.0', 'block_editor_rest_api_preload_paths');
    }
    if (empty($preload_paths)) {
        return;
    }
    /*
     * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
     * API data is preloaded.
     * Because API preloading can call the_content and other filters, plugins
     * can unexpectedly modify the global $post or enqueue assets which are not
     * intended for the block editor.
     */
    $backup_global_post = (!empty($post)) ? clone $post : $post;
    $backup_wp_scripts = (!empty($wp_scripts)) ? clone $wp_scripts : $wp_scripts;
    $backup_wp_styles = (!empty($wp_styles)) ? clone $wp_styles : $wp_styles;
    foreach ($preload_paths as &$path) {
        if (is_string($path) && !str_starts_with($path, '/')) {
            $path = '/' . $path;
            continue;
        }
        if (is_array($path) && is_string($path[0]) && !str_starts_with($path[0], '/')) {
            $path[0] = '/' . $path[0];
        }
    }
    unset($path);
    $preload_data = array_reduce($preload_paths, 'rest_preload_api_request', array());
    // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
    $post = $backup_global_post;
    $wp_scripts = $backup_wp_scripts;
    $wp_styles = $backup_wp_styles;
    wp_add_inline_script('wp-api-fetch', sprintf('wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode($preload_data)), 'after');
}

WordPress Version: 6.1

/**
 * Preloads common data used with the block editor by specifying an array of
 * REST API paths that will be preloaded for a given block editor context.
 *
 * @since 5.8.0
 *
 * @global WP_Post    $post       Global post object.
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
 *
 * @param string[]                $preload_paths        List of paths to preload.
 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 */
function block_editor_rest_api_preload(array $preload_paths, $block_editor_context)
{
    global $post, $wp_scripts, $wp_styles;
    /**
     * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
     *
     * @since 5.8.0
     *
     * @param string[]                $preload_paths        Array of paths to preload.
     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
     */
    $preload_paths = apply_filters('block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context);
    if (!empty($block_editor_context->post)) {
        $selected_post = $block_editor_context->post;
        /**
         * Filters the array of paths that will be preloaded.
         *
         * Preload common data by specifying an array of REST API paths that will be preloaded.
         *
         * @since 5.0.0
         * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
         *
         * @param string[] $preload_paths Array of paths to preload.
         * @param WP_Post  $selected_post Post being edited.
         */
        $preload_paths = apply_filters_deprecated('block_editor_preload_paths', array($preload_paths, $selected_post), '5.8.0', 'block_editor_rest_api_preload_paths');
    }
    if (empty($preload_paths)) {
        return;
    }
    /*
     * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
     * API data is preloaded.
     * Because API preloading can call the_content and other filters, plugins
     * can unexpectedly modify the global $post or enqueue assets which are not
     * intended for the block editor.
     */
    $backup_global_post = (!empty($post)) ? clone $post : $post;
    $backup_wp_scripts = (!empty($wp_scripts)) ? clone $wp_scripts : $wp_scripts;
    $backup_wp_styles = (!empty($wp_styles)) ? clone $wp_styles : $wp_styles;
    foreach ($preload_paths as &$path) {
        if (is_string($path) && !str_starts_with($path, '/')) {
            $path = '/' . $path;
            continue;
        }
        if (is_array($path) && is_string($path[0]) && !str_starts_with($path[0], '/')) {
            $path[0] = '/' . $path[0];
        }
    }
    unset($path);
    $preload_data = array_reduce($preload_paths, 'rest_preload_api_request', array());
    // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
    $post = $backup_global_post;
    $wp_scripts = $backup_wp_scripts;
    $wp_styles = $backup_wp_styles;
    wp_add_inline_script('wp-api-fetch', sprintf('wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode($preload_data)), 'after');
}

WordPress Version: 9.1

/**
 * Preloads common data used with the block editor by specifying an array of
 * REST API paths that will be preloaded for a given block editor context.
 *
 * @since 5.8.0
 *
 * @global WP_Post    $post       Global post object.
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
 *
 * @param string[]                $preload_paths        List of paths to preload.
 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 *
 * @return void
 */
function block_editor_rest_api_preload(array $preload_paths, $block_editor_context)
{
    global $post, $wp_scripts, $wp_styles;
    /**
     * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
     *
     * @since 5.8.0
     *
     * @param string[]                $preload_paths        Array of paths to preload.
     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
     */
    $preload_paths = apply_filters('block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context);
    if (!empty($block_editor_context->post)) {
        $selected_post = $block_editor_context->post;
        /**
         * Filters the array of paths that will be preloaded.
         *
         * Preload common data by specifying an array of REST API paths that will be preloaded.
         *
         * @since 5.0.0
         * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
         *
         * @param string[] $preload_paths Array of paths to preload.
         * @param WP_Post  $selected_post Post being edited.
         */
        $preload_paths = apply_filters_deprecated('block_editor_preload_paths', array($preload_paths, $selected_post), '5.8.0', 'block_editor_rest_api_preload_paths');
    }
    if (empty($preload_paths)) {
        return;
    }
    /*
     * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
     * API data is preloaded.
     * Because API preloading can call the_content and other filters, plugins
     * can unexpectedly modify the global $post or enqueue assets which are not
     * intended for the block editor.
     */
    $backup_global_post = (!empty($post)) ? clone $post : $post;
    $backup_wp_scripts = (!empty($wp_scripts)) ? clone $wp_scripts : $wp_scripts;
    $backup_wp_styles = (!empty($wp_styles)) ? clone $wp_styles : $wp_styles;
    foreach ($preload_paths as &$path) {
        if (is_string($path) && !str_starts_with($path, '/')) {
            $path = '/' . $path;
            continue;
        }
        if (is_array($path) && is_string($path[0]) && !str_starts_with($path[0], '/')) {
            $path[0] = '/' . $path[0];
        }
    }
    unset($path);
    $preload_data = array_reduce($preload_paths, 'rest_preload_api_request', array());
    // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
    $post = $backup_global_post;
    $wp_scripts = $backup_wp_scripts;
    $wp_styles = $backup_wp_styles;
    wp_add_inline_script('wp-api-fetch', sprintf('wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode($preload_data)), 'after');
}

WordPress Version: 5.9

/**
 * Preloads common data used with the block editor by specifying an array of
 * REST API paths that will be preloaded for a given block editor context.
 *
 * @since 5.8.0
 *
 * @global WP_Post $post Global post object.
 *
 * @param string[]                $preload_paths        List of paths to preload.
 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 *
 * @return void
 */
function block_editor_rest_api_preload(array $preload_paths, $block_editor_context)
{
    global $post;
    /**
     * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
     *
     * @since 5.8.0
     *
     * @param string[]                $preload_paths        Array of paths to preload.
     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
     */
    $preload_paths = apply_filters('block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context);
    if (!empty($block_editor_context->post)) {
        $selected_post = $block_editor_context->post;
        /**
         * Filters the array of paths that will be preloaded.
         *
         * Preload common data by specifying an array of REST API paths that will be preloaded.
         *
         * @since 5.0.0
         * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
         *
         * @param string[] $preload_paths Array of paths to preload.
         * @param WP_Post  $selected_post Post being edited.
         */
        $preload_paths = apply_filters_deprecated('block_editor_preload_paths', array($preload_paths, $selected_post), '5.8.0', 'block_editor_rest_api_preload_paths');
    }
    if (empty($preload_paths)) {
        return;
    }
    /*
     * Ensure the global $post remains the same after API data is preloaded.
     * Because API preloading can call the_content and other filters, plugins
     * can unexpectedly modify $post.
     */
    $backup_global_post = (!empty($post)) ? clone $post : $post;
    foreach ($preload_paths as &$path) {
        if (is_string($path) && !str_starts_with($path, '/')) {
            $path = '/' . $path;
            continue;
        }
        if (is_array($path) && is_string($path[0]) && !str_starts_with($path[0], '/')) {
            $path[0] = '/' . $path[0];
        }
    }
    unset($path);
    $preload_data = array_reduce($preload_paths, 'rest_preload_api_request', array());
    // Restore the global $post as it was before API preloading.
    $post = $backup_global_post;
    wp_add_inline_script('wp-api-fetch', sprintf('wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode($preload_data)), 'after');
}

WordPress Version: 5.8

/**
 * Preloads common data used with the block editor by specifying an array of
 * REST API paths that will be preloaded for a given block editor context.
 *
 * @since 5.8.0
 *
 * @global WP_Post $post Global post object.
 *
 * @param array                   $preload_paths        List of paths to preload.
 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
 *
 * @return void
 */
function block_editor_rest_api_preload(array $preload_paths, $block_editor_context)
{
    global $post;
    /**
     * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
     *
     * @since 5.8.0
     *
     * @param string[] $preload_paths Array of paths to preload.
     */
    $preload_paths = apply_filters('block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context);
    if (!empty($block_editor_context->post)) {
        $selected_post = $block_editor_context->post;
        /**
         * Filters the array of paths that will be preloaded.
         *
         * Preload common data by specifying an array of REST API paths that will be preloaded.
         *
         * @since 5.0.0
         * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.
         *
         * @param string[] $preload_paths Array of paths to preload.
         * @param WP_Post  $selected_post Post being edited.
         */
        $preload_paths = apply_filters_deprecated('block_editor_preload_paths', array($preload_paths, $selected_post), '5.8.0', 'block_editor_rest_api_preload_paths');
    }
    if (empty($preload_paths)) {
        return;
    }
    /*
     * Ensure the global $post remains the same after API data is preloaded.
     * Because API preloading can call the_content and other filters, plugins
     * can unexpectedly modify $post.
     */
    $backup_global_post = (!empty($post)) ? clone $post : $post;
    $preload_data = array_reduce($preload_paths, 'rest_preload_api_request', array());
    // Restore the global $post as it was before API preloading.
    $post = $backup_global_post;
    wp_add_inline_script('wp-api-fetch', sprintf('wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode($preload_data)), 'after');
}