• Resolved Nikhil Gudadhe

    (@nikhilgudadhe)


    I want to enable caching for this custom endpoint which takes search_term to find matching posts in the db.

    For example: https://site.com/wp-json/custom/v1/search/{search_term}

    add_action('rest_api_init', 'RegisterSearch');
    
    function RegisterSearch() {
    ? register_rest_route('custom/v1', 'search/(?P<search_term>[\w-]+)', array(
    
    ? ? 'methods' ?=> 'GET',
    ? ? 'callback' => 'getSearchResults',
    ? ));
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @nikhilgudadhe

    Thank you for using our plugin!

    Have you read our FAQ? Specifically the section “Can I register my own endpoint for caching?” This explains how you can add your own custom endpoints for caching.

    Thread Starter Nikhil Gudadhe

    (@nikhilgudadhe)

    Hello Richard, thank you for helping me, it worked.

    function enable_custom_search_endpoint($allowed_endpoints) {
    
      if ( ! isset($allowed_endpoints['custom/v1']) || ! in_array('search', $allowed_endpoints['custom/v1'])) {
          $allowed_endpoints['custom/v1'][] = 'search';
      }
      return $allowed_endpoints;
    }
    
    add_filter('wp_rest_cache/allowed_endpoints', 'enable_custom_search_endpoint', 10, 1);
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Unable to enable caching for custom end point’ is closed to new replies.