superjin001
Forum Replies Created
-
Hi @richard, thank you for your reply, sorry, I probably didn’t describe my needs clearly.
I have implemented caching for my custom API as your document. I want to determine within my custom API whether this particular request is cached or not when there is no cache for it. My custom API(getinfo) relies on an external website’s API, which can sometimes be unreliable. I want to avoid caching my API when this external API is not functioning correctly.function getinfo() { // Send request to external API $external_api_response = wp_remote_get('https://external-api.com/data'); // Check if the external API response is valid if (is_wp_error($external_api_response) || wp_remote_retrieve_response_code($external_api_response) !== 200) { add_filter('wp_rest_cache/skip_caching', '__return_true'); add_filter('wp_rest_cache/skip_caching', 'true'); add_filter('wp_rest_cache/skip_caching', true); } ..............other code............ } // Register custom API routing add_action('rest_api_init', function() { register_rest_route('wp/v2', '/getinfo/', array( 'methods' => 'GET', 'callback' => 'getinfo', )); }); /** * Register the /wp-json/wp/v2/getinfo endpoint so it will be cached. */ function wprc_add_acf_posts_endpoint($allowed_endpoints) { if (!isset($allowed_endpoints['wp/v2']) || !in_array('getinfo', $allowed_endpoints['wp/v2'])) { $allowed_endpoints['wp/v2'][] = 'getinfo'; } return $allowed_endpoints; } add_filter('wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
As shown in the code, I tried these three( add_filter(‘wp_rest_cache/skip_caching’, ‘__return_true’); add_filter(‘wp_rest_cache/skip_caching’, ‘true’); add_filter(‘wp_rest_cache/skip_caching’, true); ) but none of them worked, could you please tell me any way to meet my needs, thank you in advance!
- This reply was modified 1 year, 4 months ago by superjin001.
- This reply was modified 1 year, 4 months ago by superjin001.
- This reply was modified 1 year, 4 months ago by superjin001.
Forum: Plugins
In reply to: [WP REST Cache] can every cache have different cache timeoutHi @Richard Korthuis?, thank you for your quick reply, I tried the way you told, and it works, I didn’t expect a free plugin can be made so good and support quick Q&A, thank you again!