Support » Plugin: WP REST Cache » How to flush cache on custom endpoints?

  • Resolved jaxwtf

    (@jaxwtf)


    Hi, I have some custom endpoints and the cache works fine. But how can I tell plugin to flush when I save or update? Now the cache is stuck.

    I added them in this way:

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
    
      if(!isset($allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'] = [];
    
      if(!in_array('general', $allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'][] = 'general';
    
      if(!in_array('content', $allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'][] = 'content';
    
      if(!in_array('agents', $allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'][] = 'agents';
    
      if(!in_array('navigation', $allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'][] = 'navigation';
    
      if(!in_array('office', $allowed_endpoints['bra/v1']))
        $allowed_endpoints['bra/v1'][] = 'office';    
        
      return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @jaxwtf

    Thank you for using our plugin!

    There are several possible answers to your question:

    1. You say you want it to flush when you save or update, are these endpoints returning (custom) post types (or taxonomies)? If so then possibly the plugin can not detect the correct object type.

    The plugin will try to detect the object types of entries in your endpoint, but this detection is not flawless. It will check for the existence of the fields id and type and use the type as the object type (or check for id and taxonomy in the case of a taxonomy). If those fields aren’t present it will not be able to detect the object type.

    You can however ‘help’ the plugin to detect the correct object type by using the wp_rest_cache/determine_object_type filter (see the FAQ).

    Once the plugin knows the correct object type it will automatically flush the caches upon saving such a object type.

    2. If the endpoint isn’t returning (custom) post types (or taxonomies) I would still recommend using the wp_rest_cache/determine_object_type filter hook to help the plugin detect the object type.

    Once you have done that, upon saving or updating you can programmatically flush all caches of a specific object type
    Example:
    \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_object_type_caches( 'products' );
    This deletes all caches for object type products.

    3. The last option is to (upon saving or updating) programmatically flush all caches of a specific endpoint. So for instance if you want to flush the caches for the endpoint /wp-json/wp/v2/pages you can do so like this:
    \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/wp/v2/pages' );

    This function accepts two extra parameters:
    1. $strictness: This defines how strict you want the caches to match, you have three options for this:
    'strict': Delete caches by an endpoint path matching only this exact same endpoint path (and query params).
    'params': Delete caches by an endpoint path matching the exact same endpoint path with any query params that might have been used.
    'loose': Delete caches by an endpoint path matching any cache that was called starting with the endpoint path (ignoring any query params or subpaths following the given path).

    2. $force: Should the caches be deleted ($force = true) or just flushed ($force = false).

    Plugin Author Richard Korthuis

    (@rockfire)

    This thread has been marked as resolved due to lack of activity.

    You’re always welcome to open a new topic.

    Thanks for understanding!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.