• Resolved dominic_ks

    (@dominic_ks)


    Hello,

    I am adding a custom route to the cache and noticed that GET requests for a single item were not being cached, so I’m wondering if I can, or need, do something like this?

    
    function add_cachable_endpoints( $allowed_endpoints ) {
      
      $allowed_endpoints['bbp-api/v1][] = 'forums',
      $allowed_endpoints['bbp-api/v1][] = 'forums/(?P<id>\d+)',
      
      return $allowed_endpoints;
      
    }
    
    add_filter( 'wp_rest_cache/allowed_endpoints' , 'add_cachable_endpoints' , 100 , 1 );
    

    In this case a call to /wp-json/bbp-api/v1/forums/ works fine, but a call to /wp-json/bbp-api/v1/forums/7028 is not cached, so I’m wondering if this is supported… I assumed it would be since things like /wp-json/wp/v2/posts/1 are cached.

    Cheers,

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

    (@rockfire)

    Hi @dominic_ks

    The line $allowed_endpoints['bbp-api/v1][] = 'forums'; should be enough to have both /wp-json/bbp-api/v1/forums/ as /wp-json/bbp-api/v1/forums/7028 be cached by our plugin. We use the allowed endpoint to check if the endpoint that is being called starts with the allowed endpoint string. So anything that comes after /wp-json/bbp-api/v1/forums/ should be cached automatically.

    By the way in you post you have comma’s in stead of semicolons at the end of the two $allowed_endpoints... lines. Was it just in your example or also in your code?

    Thread Starter dominic_ks

    (@dominic_ks)

    Hello,

    Yes those commas are just in the I’d tried to simplify my code for this ticket and those are part of an array normally.

    Anyway, yes you’re right this does work as you say it should. The issue actually seemed to be that I was using the wp_rest_cache/determine_object_type filter and was using this check:

    
    if ( $object_type !== 'unknown' ) {
      return $object_type;
    }
    

    i.e. to just return the $object_type if one was set. Weirdly for this forums call it $object_type was coming through as NULL so I just updated that part.

    No idea why that was preventing these from being cached at all but there we go!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Route with Regex?’ is closed to new replies.