Halloween Sale 15% off Ends in See the plans
PHP

Cache Public REST API Responses for Visitors

Stores the responses of public GET requests to the REST API (such as post lists used by headless front ends and block widgets) for a few minutes for logged-out visitors, and clears the cache when content changes.

Don’t have WPIDE yet? Install it on your site

Code

// Route prefixes to cache, and for how long (seconds).
$wpide_rest_cache_routes = array( '/wp/v2/posts', '/wp/v2/pages', '/wp/v2/categories', '/wp/v2/tags', '/wp/v2/media' );
$wpide_rest_cache_ttl    = 5 * MINUTE_IN_SECONDS;

$wpide_rest_cache_key = function ( WP_REST_Request $request ) use ( $wpide_rest_cache_routes ) {
    if ( 'GET' !== $request->get_method() || is_user_logged_in() || isset( $request['_embed'] ) ) {
        return '';
    }

    foreach ( $wpide_rest_cache_routes as $prefix ) {
        if ( 0 === strpos( $request->get_route(), $prefix ) ) {
            $params = $request->get_query_params();
            ksort( $params );
            return 'wpide_rest_' . (int) get_option( 'wpide_rest_cache_gen', 1 ) . '_' . md5( $request->get_route() . wp_json_encode( $params ) );
        }
    }

    return '';
};

add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) use ( $wpide_rest_cache_key ) {
    $key = $wpide_rest_cache_key( $request );

    if ( null !== $result || ! $key ) {
        return $result;
    }

    $cached = get_transient( $key );

    if ( is_array( $cached ) ) {
        $response = new WP_REST_Response( $cached['data'], $cached['status'] );
        $response->set_headers( array_merge( $cached['headers'], array( 'X-WPIDE-Cache' => 'HIT' ) ) );
        return $response;
    }

    return $result;
}, 10, 3 );

add_filter( 'rest_request_after_callbacks', function ( $response, $handler, $request ) use ( $wpide_rest_cache_key, $wpide_rest_cache_ttl ) {
    $key = $wpide_rest_cache_key( $request );

    if ( $key && $response instanceof WP_REST_Response && 200 === $response->get_status() ) {
        $data = rest_get_server()->response_to_data( $response, false );
        set_transient( $key, array( 'data' => $data, 'status' => 200, 'headers' => $response->get_headers() ), $wpide_rest_cache_ttl );
    }

    return $response;
}, 99, 3 );

$wpide_rest_cache_flush = function () {
    update_option( 'wpide_rest_cache_gen', (int) get_option( 'wpide_rest_cache_gen', 1 ) + 1, false );
};
add_action( 'save_post', $wpide_rest_cache_flush );
add_action( 'deleted_post', $wpide_rest_cache_flush );
add_action( 'edited_term', $wpide_rest_cache_flush );
add_action( 'delete_attachment', $wpide_rest_cache_flush );

Notes

Only routes in $wpide_rest_cache_routes are cached and requests using _embed are skipped; with a persistent object cache the entries live in memory instead of the database.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.