This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
1. **Introduction**
Ever been in a situation where you need to clean up the term cache in WordPress? Well, my friend, the `clean_term_cache` action is here to save the day! This hook fires once after each taxonomy’s term cache has been cleaned. You can register it using `add_action` in your theme’s `functions.php` or, better yet, in a custom WordPress Plugin to ensure nothing breaks when you update your theme. Let’s dive into some examples on how to use this action effectively.
2. **Parameters**
Below are the 3 parameters required to use this hook:
– **$ids**: (array) An array of term IDs.
– **$taxonomy**: (string) Taxonomy slug.
– **$clean_taxonomy**: (bool) Whether or not to clean taxonomy-wide caches.
3. **Live Examples**
### Example 1: Basic Usage
Here’s a basic example of how you can use the `clean_term_cache` action hook.
function weplugins_execute_on_clean_term_cache_event($ids, $taxonomy, $clean_taxonomy){ // Code to be executed when the action occurs } // Add the action add_action("clean_term_cache", "weplugins_execute_on_clean_term_cache_event", 10, 3);
### Example 2: Logging the Cleaned Cache
In this example, we log the cleaned term cache details to the debug log.
function weplugins_log_clean_term_cache($ids, $taxonomy, $clean_taxonomy){ if (WP_DEBUG) { error_log("Term cache cleaned. IDs: " . implode(',', $ids) . " | Taxonomy: $taxonomy | Clean Taxonomy: " . ($clean_taxonomy ? 'Yes' : 'No')); } } // Add the action add_action("clean_term_cache", "weplugins_log_clean_term_cache", 10, 3);
### Example 3: Conditional Cache Cleaning
This example demonstrates conditionally handling the term cache cleaning based on the taxonomy type.
function weplugins_conditional_clean_term_cache($ids, $taxonomy, $clean_taxonomy){ if ($taxonomy === 'category') { // Perform some action specific to category taxonomy } } // Add the action add_action("clean_term_cache", "weplugins_conditional_clean_term_cache", 10, 3);
4. **Removing a Hook Callback**
To remove a hook callback, use the example below.
remove_action("clean_term_cache", "weplugins_execute_on_clean_term_cache_event", 10, 3);
Make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
5. **Contact Us**
If you’re having any trouble using this hook or need some customization, feel free to contact us. We’re here to help!
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.