wppaste
WordPress

set_site_transient( string $transient, mixed $value, int $expiration = 0 ): bool

Since
2.9.0
Source
wp-includes/option.php:2641
Sets/updates the value of a site transient.

Description

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.

Parameters

$transientstring
Transient name. Expected to not be SQL-escaped. Must be 167 characters or fewer in length.
$valuemixed
Transient value. Expected to not be SQL-escaped.
$expirationintoptional
Time until expiration in seconds. Default 0 (no expiration).Default: 0

Return value

bool
True if the value was set, false otherwise.

Performance profile

How much work a call to set_site_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_site_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
33–76

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 97.

Plugin surface
5 hooks

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

Called by
20

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

What it touches

  • transienttransientset_site_transient()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_set()called directly
  • optionnetwork optionget_site_option()called directly

Further down the call graph this can also reach serialize and query. Those are 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 set_site_transient() can have, taken from its control-flow graph on PHP 8.4.

WhenInstructionsCalls it makes
wp_using_ext_object_cache()33apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_cache_set()
!wp_using_ext_object_cache() && wp_installing()36apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_set()
!wp_using_ext_object_cache() && !wp_installing()46apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), update_site_option()
!wp_using_ext_object_cache() && !wp_installing()47apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), add_site_option()
!wp_using_ext_object_cache() && !wp_installing()53apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), update_site_option(), update_site_option()
!wp_using_ext_object_cache() && !wp_installing()54apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), add_site_option(), add_site_option()
wp_using_ext_object_cache()55apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_cache_set(), do_action(), do_action()
!wp_using_ext_object_cache() && wp_installing()58apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_set(), do_action(), do_action()
!wp_using_ext_object_cache() && !wp_installing()68apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), update_site_option(), do_action(), do_action()
!wp_using_ext_object_cache() && !wp_installing()69apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), add_site_option(), do_action(), do_action()
!wp_using_ext_object_cache() && !wp_installing()75apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), update_site_option(), update_site_option(), do_action(), do_action()
!wp_using_ext_object_cache() && !wp_installing()76apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), add_site_option(), add_site_option(), do_action(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3 and 8.4: 97 instructions, 33–76 executed per call, 6 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 · 5

5 hooks fire while set_site_transient() runs, in this order:

  1. apply_filters( pre_set_site_transient_{$transient} )filterline 2654 (+13 into the body)

    Filters the value of a specific site transient before it is set.

  2. apply_filters( expiration_of_site_transient_{$transient} )filterline 2669 (+28 into the body)

    Filters the expiration for a site transient before its value is set.

  3. do_action( set_site_transient_{$transient} )actionline 2705 (+64 into the body)

    Fires after the value for a specific site transient has been set.

  4. do_action( set_site_transient )actionline 2716 (+75 into the body)

    Fires after the value for a site transient has been set.

  5. do_action( setted_site_transient )action_deprecatedline 2728 (+87 into the body)

    Fires after the value for a site transient has been set.

Uses · 10

Used by · 20

Show all 20

Source code

function set_site_transient( $transient, $value, $expiration = 0 ) { 	/**	 * Filters the value of a specific site transient before it is set.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * @since 3.0.0	 * @since 4.4.0 The `$transient` parameter was added.	 *	 * @param mixed  $value     New value of site transient.	 * @param string $transient Transient name.	 */	$value = apply_filters( "pre_set_site_transient_{$transient}", $value, $transient ); 	$expiration = (int) $expiration; 	/**	 * Filters the expiration for a site transient before its value is set.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * @since 4.4.0	 *	 * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.	 * @param mixed  $value      New value of site transient.	 * @param string $transient  Transient name.	 */	$expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient ); 	if ( wp_using_ext_object_cache() || wp_installing() ) {		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );	} else {		$transient_timeout = '_site_transient_timeout_' . $transient;		$option            = '_site_transient_' . $transient;		wp_prime_site_option_caches( array( $option, $transient_timeout ) ); 		if ( false === get_site_option( $option ) ) {			if ( $expiration ) {				add_site_option( $transient_timeout, time() + $expiration );			}			$result = add_site_option( $option, $value );		} else {			if ( $expiration ) {				update_site_option( $transient_timeout, time() + $expiration );			}			$result = update_site_option( $option, $value );		}	} 	if ( $result ) { 		/**		 * Fires after the value for a specific site transient has been set.		 *		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.		 *		 * @since 3.0.0		 * @since 4.4.0 The `$transient` parameter was added		 *		 * @param mixed  $value      Site transient value.		 * @param int    $expiration Time until expiration in seconds.		 * @param string $transient  Transient name.		 */		do_action( "set_site_transient_{$transient}", $value, $expiration, $transient ); 		/**		 * Fires after the value for a site transient has been set.		 *		 * @since 6.8.0		 *		 * @param string $transient  The name of the site transient.		 * @param mixed  $value      Site transient value.		 * @param int    $expiration Time until expiration in seconds.		 */		do_action( 'set_site_transient', $transient, $value, $expiration ); 		/**		 * Fires after the value for a site transient has been set.		 *

Changelog

Introduced in 2.9.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.