• Hello

    We have added the below code:

    function wprc_add_acf_posts_endpoint( $allowed_endpoint ) {
    if ( ! isset( $allowed_endpoint[ 'custom/v2' ] ) || ! in_array( 'validateApi', $allowed_endpoint[ 'custom/v2' ] ) ) {
    $allowed_endpoint[ 'custom/v2' ][] = 'validateApi';
    }
    return $allowed_endpoint;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
    function wprc_add_cacheable_request_headers( $cacheable_headers ) {
    $cacheable_headers['custom/v2'] = 'X-custom-AUTH';
    return $cacheable_headers;
    }
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_add_cacheable_request_headers', 10, 1);

    We are using this custom endpoint request :

    https://customstg.wpengine.com/wp-json/custom/v2/validateApi?apiKey=[randomkey]&package=[package] with GET method. 

    We are passing headers with this key: X-custom-AUTH

    But it still does not show in the Endpoint API Caches listing.

    Could you please suggest what do we need to make correct?

    • This topic was modified 1 year, 9 months ago by gauravbj.
Viewing 1 replies (of 1 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @gauravbj ,

    Thank you for using our plugin!

    I have done some quick testing with the code you provided (and created a dummy endpoint with your namespace/endpoint). It is caching without any problems.
    So I am guessing something else is causing the endpoint not to be cached. Are you by any chance using a nonce in your call to the REST API? Because by default calls with a nonce aren’t cached. You can have them cached by adding this code:

    /**
    * Always allow caching of requests with a nonce.
    *
    * @param bool             $skip_nonce_caching False if cache should not be skipped when nonce is present.
    * @param \WP_REST_Request $request The current REST Request.
    * @param string           $request_uri The REST URI that is being requested.
    *
    * @return boolean
    */
    function wprc_skip_nonce_caching( $skip_nonce_caching, $request, $request_uri ) {
    $skip_nonce_caching = false;
    
    return $skip_nonce_caching;
    }
    
    add_filter( 'wp_rest_cache/skip_nonce_caching', 'wprc_skip_nonce_caching', 10, 3 );
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Endpoint Not caching with Rest API’ is closed to new replies.