Clear the Cache by Calling the Function Hook

Clear the Cache by Calling the Function Hook

by admin

You can use hooks to clear cache at specific points in the execution of the code. By strategically placing cache flush hooks in your code, you can ensure that caches are refreshed when needed.

1. Clearing All Cache

Simply call this function in your theme’s functions.php file or in a custom plugin to clear the entire cache. Clearing all cache triggers the “preload” feature and the preload feature starts working if the preload feature is enabled.

// Clear all cache
wpfc_clear_all_cache();

2. Clearing Cache for All Sites

If you’re running a WordPress Multisite installation and want to clear the cache for all sites within the network, you can use the following function. This function is useful in multisite environments where changes affect multiple sites, ensuring that the cache is cleared network-wide.

// Clear cache for all sites
wpfc_clear_all_site_cache();

3. Clearing All Cache and Minified Resources

This is beneficial when you’ve made changes to your stylesheet or javascript files, ensuring that visitors receive the most recent updates to these files.

// Clear all cache with minified sources
wpfc_clear_all_cache(true);

4. Clear Cache for Posts, Pages, or Custom Pages

This function takes a single parameter – the ID of the post, page, or custom page for which you want to clear the cache. The cache of the post’s tags, archive, and category is also cleared.

The example provided demonstrates how to clear the cache for a post with ID 1923:

// Replace 1923 with the ID of the content you want to clear the cache for
wpfc_clear_post_cache_by_id(1923);

Important Notice for WPML Users

If you are using the WPML plugin, you should disable the ‘Make themes work multilingual’ option as shown below. Otherwise, it will return the default language URL.

Related articles

Placeholder Image
How to Serve Cache Only via PHP

On servers using apache, the cache is served via htaccess thanks to the rewrite rule. It is the best method…

Placeholder Image
Clear Cache After Posting Comment

The cache of the page is cleared automatically after the comment is approved of.

Placeholder Image
Redirection to /wp-content/cache/all/index.html Problem

If there is a 301 redirection rule in htaccess, apache may redirect the url to http://yourdomain.com/wp-content/cache/all/index.html.

Ready to get started?

Purchase your first license and see why 1,500,000+ websites globally around the world trust us.