Using WordPress ‘clean_site_details_cache()’ PHP function

The clean_site_details_cache() WordPress PHP function is used to clear the site details cache for a specific site.

Usage

Here’s a basic example of how to use the clean_site_details_cache() function:

clean_site_details_cache(2);

In this case, the function will clear the site details cache for the site with the ID of 2.

Parameters

  • $site_id (int) – Optional. The ID of the site for which the details cache needs to be cleared. By default, it uses the current site ID.

More Information

See WordPress Developer Resources: clean_site_details_cache()

The clean_site_details_cache() function has been implemented in WordPress since version 3.7.0. It is not deprecated and its source code can be found in wp-includes/ms-blogs.php.

Examples

Clearing Cache for Current Site

In this case, we’re clearing the site details cache for the current site.

// Clear the site details cache for the current site
clean_site_details_cache();

Clearing Cache for Site ID 3

Here, we’re specifying the site ID as 3.

// Clear the site details cache for site ID 3
clean_site_details_cache(3);

Using within a function

You might want to clear cache within a function after performing some actions.

function update_site_details($site_id) {
    // Some actions that update site details...

    // Clear the site details cache
    clean_site_details_cache($site_id);
}

Clearing Cache for Multiple Sites

In this example, we’re clearing the site details cache for multiple sites using a loop.

$site_ids = array(1, 2, 3, 4, 5);

foreach($site_ids as $site_id) {
    clean_site_details_cache($site_id);
}

Clearing Cache Conditionally

Sometimes, you might want to clear the cache conditionally, based on some criteria.

$site_id = get_current_blog_id();

if($site_id > 2) {
    clean_site_details_cache($site_id);
}

In this case, we’re clearing the site details cache for the current site only if the site ID is greater than 2.