Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use clean_site_cache action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 12, 2022
5 minutes read

clean_site_cache action

Fires immediately after a site has been removed from the object cache.

To use clean_site_cache action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

In the below live example, we have defined a function execute_on_clean_site_cache_event which takes 3 parameters and we registered using add_action. The first parameter clean_site_cache is name of the hook, The second parameter execute_on_clean_site_cache_event is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_action to remove clean_site_cache action.

Parameters

    Below are the 3 parameters required to use this hook.

  • $id : (string) Site ID as a numeric string.
  • $blog : (WP_Site) Site object.
  • $domain_path_key : (string) md5 hash of domain and path.

Live Examples

Example 1: Basic Usage

Here’s a basic example of how you can use the clean_site_cache action hook to perform a task when a site is removed from the cache.

    function weplugins_execute_on_clean_site_cache_event($id, $blog, $domain_path_key){
        // You can write code here to be executed when this action occurs in WordPress. 
        // Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action("clean_site_cache", "weplugins_execute_on_clean_site_cache_event", 10, 3);
    

Example 2: Logging Cache Clearing

In this example, we log an entry every time the site cache is cleared.

    function weplugins_log_cache_clearing($id, $blog, $domain_path_key){
        error_log("Cache cleared for site ID: $id");
    }
    // add the action
    add_action("clean_site_cache", "weplugins_log_cache_clearing", 10, 3);
    

Example 3: Sending Notification

This example sends an email notification to the admin whenever the cache for a site is cleared.

    function weplugins_notify_admin_on_cache_clear($id, $blog, $domain_path_key){
        wp_mail("admin@example.com", "Site Cache Cleared", "The cache for site ID: $id has been cleared.");
    }
    // add the action
    add_action("clean_site_cache", "weplugins_notify_admin_on_cache_clear", 10, 3);
    

Removing the Hook

To remove a hook callback, use the example below.

    remove_action("clean_site_cache", "weplugins_execute_on_clean_site_cache_event", 10, 3);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.