The expiration_of_site_transient_{$transient} WordPress PHP filter lets you modify the expiration time for a site transient before its value is set. The dynamic portion of the hook name, $transient
, refers to the transient name.
Usage
add_filter('expiration_of_site_transient_'.$transient, 'your_custom_function', 10, 3); function your_custom_function($expiration, $value, $transient) { // Your custom code here return $expiration; }
Parameters
$expiration
(int): Time until expiration in seconds. Use 0 for no expiration.$value
(mixed): New value of the site transient.$transient
(string): Transient name.
More information
See WordPress Developer Resources: expiration_of_site_transient_{$transient}
Examples
Extend the expiration of a specific transient
This example extends the expiration time for the transient named “my_transient” to 1 day.
add_filter('expiration_of_site_transient_my_transient', 'extend_my_transient_expiration', 10, 3); function extend_my_transient_expiration($expiration, $value, $transient) { return DAY_IN_SECONDS; }
Set a custom expiration for all transients
This example sets a custom expiration of 2 hours for all site transients.
add_filter('expiration_of_site_transient', 'set_custom_expiration_for_all_transients', 10, 3); function set_custom_expiration_for_all_transients($expiration, $value, $transient) { return 2 * HOUR_IN_SECONDS; }
Set no expiration for specific transient
This example sets no expiration for the transient named “no_expire_transient”.
add_filter('expiration_of_site_transient_no_expire_transient', 'set_no_expiration', 10, 3); function set_no_expiration($expiration, $value, $transient) { return 0; }
Extend expiration based on transient value
This example extends the expiration time for the transient named “value_based_expiration” based on its value. If the value is more than 100, the expiration is set to 12 hours; otherwise, it’s set to 6 hours.
add_filter('expiration_of_site_transient_value_based_expiration', 'extend_expiration_based_on_value', 10, 3); function extend_expiration_based_on_value($expiration, $value, $transient) { if ($value > 100) { return 12 * HOUR_IN_SECONDS; } else { return 6 * HOUR_IN_SECONDS; } }
Shorten the expiration time for a specific transient when a condition is met
This example shortens the expiration time for the transient named “conditional_transient” to 1 hour if the value is less than 50.
add_filter('expiration_of_site_transient_conditional_transient', 'shorten_expiration_when_condition_met', 10, 3); function shorten_expiration_when_condition_met($expiration, $value, $transient) { if ($value < 50) { return HOUR_IN_SECONDS; } return $expiration; }