Exclude rest api route programmatically




Wordpress LScache Plugin: Exclude rest api route programmatically

Last Updated on: Wed, 15 Apr 2026 00:00:02
Im developing a plugin, that need to expose some rest api endpoints. I want to programmatically exclude some routes from caching. Im using JWT Auth Plugin for auth management. Im registering following route: register_rest_route( something/v1, /saved-searches, array( methods => GET, callback => listSavedSearches, )) This is how I exclude the request path/page from cache. function listSavedSearches() do_action( litespeed_control_set_nocache, nocache due to logged in ) ... //some code wp_send_json([sucess => true, results => $saved_searches],200) So far so good, I get the non-cached results fine. However, if I send the wrong auth token in the header, causing jwt_auth_invalid_token the callback listSavedSearches() never called, so it doesnt set_nocache, and goes to back as 403 unauthorised, but also its cached for future calls. All subsequent calls, even with a valid token are responded with 403. How can I avoid that? PS: If I write the path to plugin setting by going into LightSpeed Plugin > Cache > Exclude > Do Not Cache URIs It works, but Ive more than few endpoints that I want to exclude from cache, so Im looking for programmatic solution. Hi, it sounds like the hook was fired too late have you tried this approach ? function lscwp_hook() do_action( litespeed_control_set_nocache, no-cache for rest api ) add_action( litespeed_init, lscwp_hook) you can add some if check for REST API Best regards, Thank you for you reply. Ive tried the code you suggested. It works but with a problem. It does not let me specify a page / url to set_nocache. Heres how Ive tried: function lscwp_init_hook() global $wp $request = $wp->request if (endsWith($request, saved-searches )) do_action( litespeed_control_set_nocache, no-cache for rest api ) add_action( litespeed_init, lscwp_init_hook) endsWith() is utility method to search the string. So if I take do_action() out from the condition, all rest api are disabled. So it works. But I cannot seem to apply it on specific page. Any pointers? maybe something like this ? $exclude_url_list = array( test , some , thing ) foreach ($exclude_url_list as $exclude_url) if (strpos($_SERVER[REQUEST_URI], $exclude_url) !== FALSE) do_action( litespeed_control_set_nocache, no-cache for rest api ) if you have multiple URI , otherwise just one if check against REQUEST_URI ? its not completely a wordpress way , but I guess it should work. Thank you so much, this seem to work. Awesome. Now one last thing, when a cached endpoint is returned with 403 due to token expired, how can I get the correct result after token renew. For example: I get a token from JWT Auth Plugin, (login sequence) and then hit the webservice with auth bearer token. When token expires, it returns 403, I renew after login do request on same endpoint, but I always get the cached result, even for public pages. How can I tell the server to send me non-cached result or purge the existing response? or dont cache 403 responses? you can randomly attach some string , for example unix timestamp, after your request like /?rest_route=/some/thing&random or /wp-rest/some/thing?random this will force the server to generate new cache



LiteCache Rush: Speed comes from using less, not from doing it faster



Reference