The expiration_of_transient_{$transient} WordPress PHP filter allows you to modify the expiration time for a specific transient before its value is set.
Usage
add_filter('expiration_of_transient_example_transient', 'custom_expiration_time', 10, 2);
function custom_expiration_time($expiration, $value) {
// 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 transient.$transient(string): Transient name.
More information
See WordPress Developer Resources: expiration_of_transient_{$transient}
Examples
Extend the expiration time of a transient
Change the expiration time of a transient named example_transient to 2 hours.
add_filter('expiration_of_transient_example_transient', 'extend_expiration_time', 10, 2);
function extend_expiration_time($expiration, $value) {
return 2 * HOUR_IN_SECONDS;
}
Set the expiration time based on transient value
Set the expiration time of a transient named example_transient based on the length of the value (in minutes).
add_filter('expiration_of_transient_example_transient', 'expiration_based_on_value', 10, 2);
function expiration_based_on_value($expiration, $value) {
return strlen($value) * MINUTE_IN_SECONDS;
}
Set a transient to never expire
Set a transient named example_transient to never expire.
add_filter('expiration_of_transient_example_transient', 'no_expiration', 10, 2);
function no_expiration($expiration, $value) {
return 0;
}
Set different expiration times for different transients
Set different expiration times for example_transient_1 and example_transient_2.
add_filter('expiration_of_transient_example_transient_1', 'expiration_transient_1', 10, 2);
function expiration_transient_1($expiration, $value) {
return 1 * HOUR_IN_SECONDS;
}
add_filter('expiration_of_transient_example_transient_2', 'expiration_transient_2', 10, 2);
function expiration_transient_2($expiration, $value) {
return 30 * MINUTE_IN_SECONDS;
}
Set the expiration time based on the current day of the week
Set the expiration time of a transient named example_transient based on the current day of the week (longer on weekends).
add_filter('expiration_of_transient_example_transient', 'expiration_based_on_day', 10, 2);
function expiration_based_on_day($expiration, $value) {
$current_day = date('w');
// Saturday or Sunday
if ($current_day == 6 || $current_day == 0) {
return 2 * HOUR_IN_SECONDS;
}
// Monday through Friday
else {
return 1 * HOUR_IN_SECONDS;
}
}