Using WordPress ‘pre_site_transient_{$transient}’ PHP filter

‘pre_site_transient_{$transient}’ is a dynamic WordPress PHP filter that allows you to control the value of an existing site transient before it is retrieved in WordPress.

By using the “pre_site_transient_{$transient}” hook, you can modify or override the site transient value before it is accessed.

Usage

function bypass_transient_transient( $pre_site_transient, $transient ) {
    return $pre_site_transient;
}
add_filter( 'pre_site_transient_{$transient}', 'bypass_transient_transient', 10, 2 );

Parameters

  • $pre_site_transient (mixed): The default value to return if the site transient does not exist. Returning any value other than false will short-circuit the retrieval of the transient and return that value instead.
  • $transient (string): The name of the transient.

Examples

Bypassing a transient retrieval for a specific user role

function bypass_transient_for_admins( $pre_site_transient, $transient ) {
    if ( current_user_can( 'administrator' ) ) {
        return 'bypass';
    }
    return $pre_site_transient;
}
add_filter( 'pre_site_transient_transient_name', 'bypass_transient_for_admins', 10, 2 );

In this example, the function bypass_transient_for_admins() checks if the current user has the ‘administrator’ role. If they do, it returns ‘bypass’ instead of the actual site transient value. Otherwise, it returns the default $pre_site_transient.

Setting a default value for a transient

function set_default_transient_value( $pre_site_transient, $transient ) {
    return 'Default Value';
}
add_filter( 'pre_site_transient_transient_name', 'set_default_transient_value', 10, 2 );

The function set_default_transient_value() returns a default value ‘Default Value’ for the transient named ‘transient_name’ if it doesn’t exist.

Adding a prefix to a transient value

function add_prefix_to_transient( $pre_site_transient, $transient ) {
    if ( $pre_site_transient !== false ) {
        return 'Prefix - ' . $pre_site_transient;
    }
    return $pre_site_transient;
}
add_filter( 'pre_site_transient_transient_name', 'add_prefix_to_transient', 10, 2 );

This example demonstrates how to add a prefix to the value of a transient named ‘transient_name’ using the function add_prefix_to_transient().

Combining multiple transients into a single value

function combine_transients( $pre_site_transient, $transient ) {
    $transient_value1 = get_site_transient( 'transient_name_1' );
    $transient_value2 = get_site_transient( 'transient_name_2' );

    if ( $transient_value1 !== false && $transient_value2 !== false ) {
        return $transient_value1 . ' ' . $transient_value2;
    }
    return $pre_site_transient;
}
add_filter( 'pre_site_transient_combined_transient', 'combine_transients', 10, 2 );

The function combine_transients() is used to combine the values of two transients, ‘transient_name_1’ and ‘transient_name_2’, into a single value. It returns the combined value if both transients exist.

Excluding a transient value for a specific IP address

function exclude_transient_for_ip( $pre_site_transient, $transient ) {
    $ip = '123.456.789.0';

    if ( $_SERVER['REMOTE_ADDR'] === $ip ) {
        return false;
    }
    return $pre_site_transient;
}
add_filter( 'pre_site_transient_transient_name', 'exclude_transient_for_ip', 10, 2 );

In this example, the function `exclude_transient_for_ip()` checks if the current user’s IP address matches a specific IP (‘123.456.789.0’). If there is a match, it returns `false`, effectively bypassing the retrieval of the transient named ‘transient_name’. If the IP addresses do not match, it returns the default `$pre_site_transient`.