WP REST Cache

Beschreibung

Are you facing speed issues, using the WordPress REST API? This plugin will allow WordPress to cache the responses of the REST API, making it much faster.

This plugin offers:

  • Caching of all default WordPress REST API GET-endpoints.
  • Caching of (custom) post type endpoints.
  • Caching of (custom) taxonomy endpoints.
  • Automated flushing of caches if (some of) its contents are edited.
  • Manual flushing of all caches.
  • Manual flushing of specific caches.
  • A counter how many times a cache has been retrieved.
  • Specifying after what time the cache should be timed out.
  • Registering custom endpoints for caching.
  • Automatic cache regeneration.

Installation from within WordPress

  1. Visit ‚Plugins > Add New‘ (or ‚My Sites > Network Admin > Plugins > Add New‘ if you are on a multisite installation).
  2. Search for ‚WP REST Cache‘.
  3. Activate the WP REST Cache plugin through the ‚Plugins‘ menu in WordPress.
  4. Go to „after activation“ below.

Installation manually

  1. Upload the wp-rest-cache folder to the /wp-content/plugins/ directory.
  2. Activate the WP REST Cache plugin through the ‚Plugins‘ menu in WordPress.
  3. Go to „after activation“ below.

After activation

  1. Visit ‚Plugins > Must-Use‘ (or ‚My Sites > Network Admin > Plugins > Must-Use‘ if you are on a multisite installation).
  2. Check if the ‚WP REST Cache – Must-Use Plugin‘ is there, if not copy the file wp-rest-cache.php from the /sources folder of the WP REST Cache Plugin to the folder /wp-content/mu-plugins/.

Optionally:
The default timeout for caches generated by the WP REST Cache plugin is set to 1 year. If you want to change this:

  1. Visit ‚Settings > WP REST Cache‘.
  2. Change the Cache timeout.

Screenshots

  • Settings for the WP REST Cache plugin.
  • An overview of cached endpoint calls.
  • An overview of cached single items.
  • Cache details page – Cache info.
  • Cache details page – Cache data.

FAQ

I have edited a page/post, do I need to clear the cache?

No, the plugin will automatically flush all cache related to the page/post you just edited.

I have created a custom post type, will the plugin cache the custom post type endpoint?

Yes, the plugin will automatically cache the endpoint of custom post types. Unless you have created a custom WP_REST_Controller for it, then it will not automatically cache the endpoint.

I have created a custom taxonomy, will the plugin cache the taxonomy endpoint?

Yes, the plugin will automatically cache the endpoint of custom taxonomies. Unless you have created a custom WP_REST_Controller for it, then it will not automatically cache the endpoint.

I have created a custom WP REST endpoint, will the plugin cache this endpoint?

No, the plugin will not cache your custom endpoint unless you tell it to cache it using the hook wp_rest_cache/allowed_endpoints (See ‚Can I register my own endpoint for caching?‘). Please keep in mind that once you do so the plugin will not automatically flush the cache of that endpoint if something is edited (it has no way of knowing when to flush the cache). It will however try to determine the relations and for the determined relations it will flush the cache automatically once the relation is edited.

Can I register my own endpoint for caching?

Yes you can! Use the hook wp_rest_cache/allowed_endpoints like this:

/**
 * Register the /wp-json/acf/v3/posts endpoint so it will be cached.
 */
function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
    if ( ! isset( $allowed_endpoints[ 'acf/v3' ] ) || ! in_array( 'posts', $allowed_endpoints[ 'acf/v3' ] ) ) {
        $allowed_endpoints[ 'acf/v3' ][] = 'posts';
    }
    return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);

Please note: the WP REST Cache plugin will try to detect relations in the cached data to automatically flush the cache when related items are edited, but this detection is not flawless so your caches might not be flushed automatically.

Can I unregister an endpoint so it is no longer cached?

Yes you can! Use the hook wp_rest_cache/allowed_endpoints like this:

/**
 * Unregister the /wp-json/wp/v2/comments endpoint so it will not be cached.
 */
function wprc_unregister_wp_comments_endpoint( $allowed_endpoints ) {
    if ( isset( $allowed_endpoints[ 'wp/v2' ] ) && ( $key = array_search( 'comments', $allowed_endpoints[ 'wp/v2' ] ) ) !== false ) {
        unset( $allowed_endpoints[ 'wp/v2' ][ $key ] );
    }
    return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_unregister_wp_comments_endpoint', 100, 1);

Can I force a call to the REST API to not use caching?

Yes you can! Add the GET-parameter skip_cache=1 to your call and no caching will be used.

On the cache overview page I see the object type is ‚unknown‘. Can I help the WP REST Cache plugin to detect the object type correctly?

Yes you can! Use the hook wp_rest_cache/determine_object_type like this:

function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    if ( $object_type !== 'unknown' || strpos( $uri, $your_namespace . '/' . $your_rest_base ) === false ) {
        return $object_type;
    }
    // Do your magic here
    $object_type = 'website';
    // Do your magic here
    return $object_type;
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );

Can expired caches be automatically regenerated?

Yes they can! Go to Settings > WP REST Cache, on the Settings tab you can check Enable cache regeneration, this will activate a cron job which will check if there are any expired (or flushed) caches and regenerate them. Using the Regeneration interval you can determine how often this regeneration process should run. The Max number regenerate caches limits the number of regenerated caches per regeneration process, this is so your server doesn’t get flooded with the regeneration calls.

Kann ich die Option „REST-Cache löschen“ in der Admin-Leiste ausblenden?

Ja, das kannst du! Benutze den Hook wp_rest_cache/display_clear_cache_button wie folgt:

function wprc_display_clear_cache_button( $show ) {
    return false;
}
add_filter('wp_rest_cache/display_clear_cache_button', 'wprc_display_clear_cache_button', 10, 1);

Kann ich die Caches anhand der Anfrage-Header unterscheiden?

Ja, das kannst du! Dafür gibt es zwei Möglichkeiten:
1. Gehe zu Einstellungen > WP REST Cache und füge Global cachebare Anfrage-Header hinzu. Das ist eine kommagetrennte Liste. Diese Header werden für ALLE Endpunkte verwendet.
2. Benutze den Hook wp_rest_cache/cacheable_request_headers, um für jeden Endpunkt festzulegen, welche Anfrage-Header verwendet werden sollen. Zum Beispiel so:

function wprc_add_cacheable_request_headers( $cacheable_headers ) {
    $cacheable_headers['wp/v2/posts'] = 'LANG';
    return $cacheable_headers;
}
add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_add_cacheable_request_headers', 10, 1);

Kann ich ändern, welche Benutzer die Einstellungen ändern und den Cache leeren können?

Ja, das kannst du! Benutze den Hook wp_rest_cache/settings_capability wie folgt:

function wprc_change_settings_capability( $capability ) {
    // Change the capability to users who can edit posts.
    return 'edit_posts';
}
add_filter('wp_rest_cache/settings_capability', 'wprc_change_settings_capability', 10, 1);

Kann ich WP-CLI benutzen, um Caches aus der Kommandozeile heraus zu leeren?

Ja, das kannst du! Benutze den Befehl wp wp-rest-cache flush, um die Caches zu leeren. Gib wp wp-rest-cache flush --help ein, um alle Optionen zu sehen.

Is Redis Object Cache supported?

We are using the WordPress transient API, so as long as you are using a Redis Object Cache plugin which enables Redis caching through the transients API it is supported.

How can I report security bugs?

You can report security bugs through the Patchstack Vulnerability Disclosure Program. The Patchstack team helps validate, triage and handle any security vulnerabilities. Report a security vulnerability.

Rezensionen

11. April 2025 4 Antworten
Using this plugin has made my API for filtering products go from finishing the filtering and reaching the user in 2.3 seconds to reaching the user in about 200 ms. A huge improvement indeed. However, I’ve noticed two things that slowly drive me insane: The add_filter for hiding the REST API button in the admin panel isn’t working. Why does this need a filter at all? Just make it a setting. I don’t get it. There is a serious lack of settings for this plugin. What if I want to cache one API route shorter than another? That’s currently impossible. Where’s the „regenerate all caches“ button? This might very well be the only caching plugin without such a button. Yes, it has a „Clear all“, but no „Regenerate all“. This is a solid 5/5 if they add the above. I’ve yet to find a better solution that doesn’t involve Cloudflare. Thanks for the plugin!
28. April 2024
Great plugin! I use it for a headless WordPress instance with Nuxt 3, and it works perfectly fine. The only thing I needed to change was the API endpoint since I created a custom endpoint to the route. But the FAQ cleared this up quickly and it works flawlessly. As a small suggestion I’d propose to add this option to the plugin’s settings page for easier access.
12. März 2024
It’s been years since I touched wordpress or php. My usecase was very tricky and not doable out of the box. It worked out great in the end and the response time is now 4-5 times quicker. Amazing. But the best part is how quickly Richard answered to my noob questions and even fixed a bug I found. If you ever need a beta tester! Hit me up <3
7. März 2024 2 Antworten
对于 WordPress REST API 提速非常明显,非常棒! 并且还可以当发布文章、分类、Tag… 时自动刷新缓存,完全不用担心缓存时间过久问题。 但是我还有一个问题,这个缓存的数据是存储到哪里的,是存储在 Redis 中的吗?
Alle 40 Rezensionen lesen

Mitwirkende & Entwickler

„WP REST Cache“ ist Open-Source-Software. Folgende Menschen haben an diesem Plugin mitgewirkt:

Mitwirkende

„WP REST Cache“ wurde in 4 Sprachen übersetzt. Danke an die Übersetzerinnen und Übersetzer für ihre Mitwirkung.

Übersetze „WP REST Cache“ in deine Sprache.

Interessiert an der Entwicklung?

Durchstöbere den Code, sieh dir das SVN Repository an oder abonniere das Entwicklungsprotokoll per RSS.

Änderungsprotokoll

2025.1.4

Release Date: June 25th, 2025

Fix: Better checking of existing primary key before updating it.

2025.1.3

Release Date: June 25th, 2025

Fix: Check if primary key exists before dropping it.

2025.1.2

Release Date: June 11th, 2025

Fix: Make sure comment endpoints are flushed when the corresponding post is deleted or unpublished.
Improvement: Add VDP to FAQ.

2025.1.1

Release Date: June 6th, 2025

Fix: A path-traversal vulnerability in the plugin was discovered and fixed. It was reported by Darius Sveikauskas.

2025.1.0

Release Date: April 10th, 2025

Improvement: Flush media endpoint caches when a new media has been uploaded.

Earlier versions

For the changelog of earlier versions, please refer to the changelog on Github.