-
Notifications
You must be signed in to change notification settings - Fork 2
Clearing the Cache Dynamically
Rapid Cache automatically handles updating the cache in many scenarios, such as when you publish a new post, or when you change your WordPress theme. However, there may be scenarios where you need to have finer control over when the cache is cleared, such as when a plugin or theme does not include support for WordPress caching plugins.
The following examples use the WordPress API hooks and filters system to call a custom function that itself will call the Rapid Cache API to clear the cache when a specific event occurs.
While you can add this code to your theme's functions.php
file or use a Functionality Plugin, we recommend using a WordPress Must-Use Plugin by creating a file like wp-content/mu-plugins/rc-clear-cache.php
and then adding the necessary PHP code (note that if the mu-plugins
directory doesn't exist, you should create it).
What follows are a few examples of how you can clear the cache dynamically.
If you want to clear the cache every time a post is saved or updated (i.e., when the save_post
action is fired), you can use the following.
add_action( 'save_post', 'my_custom_clear_cache', 10, 1 );
function my_custom_clear_cache( ) {
rapidcache_clear_cache();
}
Using the save_post_{$post_type}
hook, which is fired whenever that custom post type is created or updated (see docs), you can use something like the following to clear the cache for a given page whenever a post with that custom post type is saved.
add_action( 'save_post_my-custom-post-type', 'clear_cache_for_page_id_5', 10, 1 );
function clear_cache_for_page_id_5( ) {
rapidcache_clear_post_cache(5); // Clears cache for Post ID 5
}
You'll want to change 5
to the ID of the Page/Post whose cache you'd like to clear when that custom post type is saved, and you'll want to change my-custom-post-type
to the actual name of your Custom Post Type.
Using the save_post
hook, which is fired whenever a post created or updated (see docs), you can use something like the following to clear the cache for a given URL on your site.
add_action( 'save_post', 'clear_cache_for_page_url', 10, 1 );
function clear_cache_for_page_url( ) {
rapidcache_clear_url_cache('http://example.com/sample-page/'); // Clears cache for page with URL http://example.com/sample-page/
}
You'll want to change http://example.com/sample-page/
to the URL of the Page/Post whose cache you'd like to clear. Note that you can also use the Watered-Down Regex Syntax in this URL.
Using the save_post
hook, which is fired whenever a post created or updated (see docs), you can use something like the following to clear the cache for a given page.
add_action( 'save_post', 'clear_cache_for_page_id_5', 10, 1 );
function clear_cache_for_page_id_5( ) {
rapidcache_clear_post_cache(5); // Clears cache for Post ID 5
}
You'll want to change 5
to the ID of the Page/Post whose cache you'd like to clear.
If you need to schedule a custom clear cache event to occur at a recurring time, you can use the WP Crontrol plugin to add a custom PHP Cron Event that makes one or more Rapid Cache API calls at a specified interval. After installing WP Crontrol, visit Dashboard → Tools → Cron Events → Add PHP Cron Event to add a new cron event.
If you need to call the Rapid Cache API from another PHP file, you will first need to load the WordPress framework (via wp-load.php
). See the examples below.
<?php
// Load WordPress framework
require_once dirname(__FILE__).'/wp-load.php';
// Any of these API calls can now be made from this PHP file
rapidcache_clear_cache(); // Clear current site cache
rapidcache_clear_post_cache($post_id); // Clear single post cache
rapidcache_clear_url_cache($url); // Clear url cache
rapidcache_wipe_cache(); // Clear entire cache (all sites if multisite)
rapidcache_purge_expired_cache(); // Clear only the expired cache files, leaving the valid intact.
rapidcache_get_version(); // Returns the plugin version
rapidcache_get_options(); // Returns the saved plugin options