Using WordPress ‘clean_page_cache’ PHP action

The clean_page_cache WordPress action fires immediately after the given page’s cache is cleaned.

Usage

add_action('clean_page_cache', 'my_custom_function');
function my_custom_function($post_id) {
  // your custom code here
}

Parameters

  • $post_id (int): The ID of the post whose cache is cleaned.

More information

See WordPress Developer Resources: clean_page_cache

Examples

Log cache cleaning event

Log the event when a page cache is cleaned.

add_action('clean_page_cache', 'log_cache_cleaning_event');
function log_cache_cleaning_event($post_id) {
  error_log("Cache cleaned for post ID: {$post_id}");
}

Send a notification

Send a notification to the admin when a page cache is cleaned.

add_action('clean_page_cache', 'send_admin_notification');
function send_admin_notification($post_id) {
  $admin_email = get_option('admin_email');
  wp_mail($admin_email, 'Cache Cleaned', "Cache cleaned for post ID: {$post_id}");
}

Update a custom cache counter

Update a custom cache counter when a page cache is cleaned.

add_action('clean_page_cache', 'update_custom_cache_counter');
function update_custom_cache_counter($post_id) {
  $cache_counter = get_option('custom_cache_counter', 0);
  update_option('custom_cache_counter', $cache_counter + 1);
}

Trigger a third-party API call

Trigger an API call to a third-party service when a page cache is cleaned.

add_action('clean_page_cache', 'trigger_third_party_api');
function trigger_third_party_api($post_id) {
  $api_url = 'https://example.com/api/v1/cache_cleaned';
  wp_remote_post($api_url, array('body' => array('post_id' => $post_id)));
}

Update a post meta value

Update a post meta value when the cache of the post is cleaned.

add_action('clean_page_cache', 'update_post_meta_on_cache_clean');
function update_post_meta_on_cache_clean($post_id) {
  update_post_meta($post_id, 'cache_last_cleaned', time());
}