TL;DR
You can use the wcdp_clear_cache()
function to clear cached values of progress bars and donation leaderboards. This does not affect caches from third-party plugins. Below are examples of how to use this function in different scenarios.
How wcdp_clear_cache()
Works
The wcdp_clear_cache()
function is a built-in utility that clears the cached values related to Donation Platform for WooCommerce, specifically:
- Progress bars: Ensures the displayed progress is up-to-date.
- Donation leaderboards: Updates rankings based on the latest data.
It does not delete cache stored by other caching plugins like WP Rocket, W3 Total Cache, or object caches like Redis.
Example Use Cases
1. Clearing Cache Programmatically
You can call wcdp_clear_cache()
anywhere in your theme or plugin code to ensure fresh donation data is displayed.
wcdp_clear_cache();
2. Clearing Cache on a Custom Schedule (Cron Job)
If you want to clear the cache periodically (e.g., every hour), you can use WordPress’ wp_schedule_event()
function.
function schedule_wcdp_cache_clear() {
if (!wp_next_scheduled('wcdp_scheduled_cache_clear')) {
wp_schedule_event(time(), 'hourly', 'wcdp_scheduled_cache_clear');
}
}
add_action('wp', 'schedule_wcdp_cache_clear');
add_action('wcdp_scheduled_cache_clear', 'wcdp_clear_cache');
3. Clearing Cache When a New Donation is Made
You might want to automatically clear the cache whenever a new donation is received to ensure the latest data is shown.
function clear_cache_on_donation($order_id) {
wcdp_clear_cache();
}
add_action('woocommerce_thankyou', 'clear_cache_on_donation');