Using WordPress ‘pre_set_site_transient_{$transient}’ PHP filter

The pre_set_site_transient_{$transient} WordPress PHP filter allows you to modify the value of a specific site transient before it is set.

Usage

add_filter( 'pre_set_site_transient_example_transient', 'my_custom_function', 10, 2 );

function my_custom_function( $value, $transient ) {
    // your custom code here
    return $value;
}

Parameters

  • $value (mixed): New value of the site transient.
  • $transient (string): Transient name.

More information

See WordPress Developer Resources: pre_set_site_transient_{$transient}

Examples

Modify a specific site transient value

This example modifies the value of the example_transient site transient:

add_filter( 'pre_set_site_transient_example_transient', 'modify_example_transient', 10, 2 );

function modify_example_transient( $value, $transient ) {
    $value = 'New value for the transient';
    return $value;
}

Add expiration time to a transient

This example adds an expiration time to the example_transient site transient:

add_filter( 'pre_set_site_transient_example_transient', 'add_expiration_to_example_transient', 10, 2 );

function add_expiration_to_example_transient( $value, $transient ) {
    $expiration = 3600; // Expiration time in seconds
    set_site_transient( $transient, $value, $expiration );
    return '__return_true'; // Bypass saving the transient again
}

Prevent a transient from being updated

This example prevents the example_transient site transient from being updated:

add_filter( 'pre_set_site_transient_example_transient', 'prevent_transient_update', 10, 2 );

function prevent_transient_update( $value, $transient ) {
    return false;
}

Log transient updates

This example logs updates to the example_transient site transient:

add_filter( 'pre_set_site_transient_example_transient', 'log_transient_updates', 10, 2 );

function log_transient_updates( $value, $transient ) {
    error_log( "Transient {$transient} has been updated to: " . print_r( $value, true ) );
    return $value;
}

Apply a custom function to the transient value

This example applies a custom function to the example_transient site transient value:

add_filter( 'pre_set_site_transient_example_transient', 'apply_custom_function_to_transient', 10, 2 );

function apply_custom_function_to_transient( $value, $transient ) {
    $value = my_custom_function( $value );
    return $value;
}

function my_custom_function( $value ) {
    // Perform your custom operation here
    return $value;
}