wppaste
WordPress

get_transient( string $transient ): mixed

Since
2.8.0
Source
wp-includes/option.php:1431
Retrieves the value of a transient.

Description

If the transient does not exist, does not have a value, or has expired, then the return value will be false.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$transientstring
Transient name. Expected to not be SQL-escaped.

Return value

mixed
Value of transient.

Performance profile

How much work a call to get_transient() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11–61

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.

Plugin surface
2 hooks

Third-party callbacks on 'pre_transient_{$transient}', 'transient_{$transient}' run inside this call, and their cost is not bounded by anything here.

Called by
23

23 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • transienttransientget_transient()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()called directly
  • optionoption read or writeget_option()called directly
  • serializeserialisationmaybe_unserialize()one call below get_transient()

Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_transient() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$pre !== false11apply_filters()
$pre === false && wp_using_ext_object_cache()26apply_filters(), wp_using_ext_object_cache(), wp_cache_get(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && isset($value)29apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && wp_installing()29apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_get(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !isset($value)33apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), get_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && isset($alloptions[$transient_option]) && isset($value)34apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing()38–46apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !isset($alloptions[$transient_option]) && $timeout === false && !isset($value)50apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), get_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !isset($alloptions[$transient_option]) && $timeout !== false && !$timeout && isset($value)50apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), time(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !isset($alloptions[$transient_option]) && $timeout !== false && !$timeout && !isset($value)54apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), time(), get_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !isset($alloptions[$transient_option]) && $timeout !== false && $timeout && isset($value)57apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), time(), delete_option(), delete_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !isset($alloptions[$transient_option]) && $timeout !== false && $timeout && !isset($value)61apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_installing(), wp_load_alloptions(), get_option(), time(), delete_option(), delete_option(), get_option(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 68 instructions, 11–61 executed per call, 8 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_transient() runs, in this order:

  1. apply_filters( pre_transient_{$transient} )filterline 1449 (+18 into the body)

    Filters the value of an existing transient before it is retrieved.

  2. apply_filters( transient_{$transient} )filterline 1491 (+60 into the body)

    Filters an existing transient's value.

Uses · 8

Used by · 23

Show all 23

Source code

function get_transient( $transient ) { 	/**	 * Filters the value of an existing transient before it is retrieved.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * Returning a value other than false from the filter will short-circuit retrieval	 * and return that value instead.	 *	 * @since 2.8.0	 * @since 4.4.0 The `$transient` parameter was added	 *	 * @param mixed  $pre_transient The default value to return if the transient does not exist.	 *                              Any value other than false will short-circuit the retrieval	 *                              of the transient, and return that value.	 * @param string $transient     Transient name.	 */	$pre = apply_filters( "pre_transient_{$transient}", false, $transient ); 	if ( false !== $pre ) {		return $pre;	} 	if ( wp_using_ext_object_cache() || wp_installing() ) {		$value = wp_cache_get( $transient, 'transient' );	} else {		$transient_option = '_transient_' . $transient;		if ( ! wp_installing() ) {			// If option is not in alloptions, it is not autoloaded and thus has a timeout.			$alloptions = wp_load_alloptions(); 			if ( ! isset( $alloptions[ $transient_option ] ) ) {				$transient_timeout = '_transient_timeout_' . $transient;				wp_prime_option_caches( array( $transient_option, $transient_timeout ) );				$timeout = get_option( $transient_timeout );				if ( false !== $timeout && $timeout < time() ) {					delete_option( $transient_option );					delete_option( $transient_timeout );					$value = false;				}			}		} 		if ( ! isset( $value ) ) {			$value = get_option( $transient_option );		}	} 	/**	 * Filters an existing transient's value.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * @since 2.8.0	 * @since 4.4.0 The `$transient` parameter was added	 *	 * @param mixed  $value     Value of transient.	 * @param string $transient Transient name.	 */	return apply_filters( "transient_{$transient}", $value, $transient );}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.