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

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

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

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

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
Clearing Cache After Deleting Post/Page

When a post or a page is deleted, some cache files are cleared instead of all the cache files being…

Create a Post Cache by ID Using the Function Hook
Create a Post Cache by ID Using the Function Hook

This function is designed for manually creating page caches.

Placeholder Image
Buffer Callback Filter

You can use the Buffer Callback Filter feature to make any changes to the html source of a page or…

Ready to get started?

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