-1

I'm using WP Rocket along with their helper plugin WP Rocket | Cache WP Rest API which allows caching of WordPress REST API endpoints.

I’ve added the following filter to allow REST API caching:

add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' );

Now it's caching API routes like:

/wp-json/wc/store/v1/cart

/wp-json/custom/v1/xyz

However, I want to exclude certain endpoints (for example, /wp-json/wp/v2/posts or /wp-json/custom/v1/sensitive-data) from being cached.

1 Answer 1

1

To exclude specific REST API endpoints from being cached by WP Rocket, you can use this in your functions.php:

add_filter( 'rocket_cache_reject_uri', function( $urls ) {
    $urls[] = '/wp-json/wp/v2/posts';
    $urls[] = '/wp-json/custom/v1/sensitive-data';
    return $urls;
});

This will skip caching for those endpoints, while everything else still gets cached normally.

Hope this will works you out!

Sign up to request clarification or add additional context in comments.

2 Comments

hi, ur solution is not working. After adding this, WP-Rocket is not collecting any data from api
Yeah if it's not caching anything after adding that, maybe you’re missing this part: add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' ); That line tells WP Rocket to allow caching REST API in the first place Without it, nothing gets cached at all no matter what

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.