set_transient( string $transient, mixed $value, int $expiration = 0 ): bool
- Since
- 2.8.0
- Source
wp-includes/option.php:1509
Description
You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.
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.
Must be 172 characters or fewer in length. $valuemixed- Transient value. Must be serializable if non-scalar.
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_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
- Scaling
- Constant
- Instructions
- 34–97
- Plugin surface
- 5 hooks
- Called by
- 22
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 132.
Third-party callbacks on 'pre_set_transient_{$transient}', 'expiration_of_transient_{$transient}', 'set_transient_{$transient}' run inside this call, and their cost is not bounded by anything here.
22 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
set_transient()this function does it - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_set()called directly - optionoption read or write
get_option()called directly - serializeserialisation
maybe_unserialize()one call below set_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 · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost set_transient() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
wp_using_ext_object_cache() | 34 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_cache_set() |
!wp_using_ext_object_cache() && wp_installing() | 37 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_set() |
!wp_using_ext_object_cache() && !wp_installing() | 44 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option() |
!wp_using_ext_object_cache() && !wp_installing() | 49 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), update_option() |
!wp_using_ext_object_cache() && !wp_installing() | 51 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), add_option() |
wp_using_ext_object_cache() | 56 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_cache_set(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 56 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), time(), update_option() |
!wp_using_ext_object_cache() && wp_installing() | 59 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_set(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 61 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), time(), add_option(), add_option() |
!wp_using_ext_object_cache() && !wp_installing() | 61 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), time(), update_option(), update_option() |
!wp_using_ext_object_cache() && !wp_installing() | 66 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 70 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), delete_option(), time(), add_option(), add_option() |
8 further outcomes, up to 97 instructions
!wp_using_ext_object_cache() && !wp_installing() | 71 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), update_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 73 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), add_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 75 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), delete_option(), time(), add_option(), add_option(), update_option() |
!wp_using_ext_object_cache() && !wp_installing() | 78 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), time(), update_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 83 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), time(), add_option(), add_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 83 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), time(), update_option(), update_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 92 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), delete_option(), time(), add_option(), add_option(), do_action(), do_action() |
!wp_using_ext_object_cache() && !wp_installing() | 97 | apply_filters(), apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_option(), get_option(), delete_option(), time(), add_option(), add_option(), update_option(), do_action(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 132 instructions, 34–97 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 · 5
5 hooks fire while set_transient() runs, in this order:
- apply_filters( pre_set_transient_{$transient} )filterline 1526 (+17 into the body)
Filters a specific transient before its value is set.
- apply_filters( expiration_of_transient_{$transient} )filterline 1539 (+30 into the body)
Filters the expiration for a transient before its value is set.
- do_action( set_transient_{$transient} )actionline 1594 (+85 into the body)
Fires after the value for a specific transient has been set.
- do_action( set_transient )actionline 1605 (+96 into the body)
Fires after the value for a transient has been set.
- do_action( setted_transient )action_deprecatedline 1618 (+109 into the body)
Fires after the transient is set.
Uses · 11
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_using_ext_object_cache()Toggles `$_wp_using_ext_object_cache` on and off without directly touching global.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- wp_cache_set()Saves the data to the cache.
- wp_prime_option_caches()Primes specific options into the cache with a single database query.
- get_option()Retrieves an option value based on an option name.
- add_option()Adds a new option.
- delete_option()Removes an option by name. Prevents removal of protected WordPress options.
- update_option()Updates the value of an option that was already added.
- do_action()Calls the callback functions that have been added to an action hook.
- do_action_deprecated()Fires functions attached to a deprecated action hook.
Used by · 22
- Featured_Content::get_featured_post_ids()Gets featured post IDs.
- RSSCache::set()
- WP_Automatic_Updater::has_fatal_error()Performs a loopback request to check for potential fatal errors.
- WP_Site_Health::wp_cron_scheduled_check()Runs the scheduled event to check and update the latest site health status for the website.
- WP_oEmbed_Controller::get_proxy_item()Callback for the proxy API endpoint.
- clean_dirsize_cache()Cleans directory size cache used by recurse_dirsize().
- install_themes_feature_list()Retrieves the list of WordPress theme features (aka theme tags).
- is_multi_author()Determines whether this site has more than one author.
- recurse_dirsize()Gets the size of a directory recursively.
- register_core_block_style_handles()Registers core block style handles.
- spawn_cron()Sends a request to run cron through HTTP request that doesn't halt page loading.
- twentyfifteen_categorized_blog()Determines whether blog/site has more than one category.
Show all 22
- twentyfourteen_categorized_blog()Finds out if blog has more than one category.
- twentyseventeen_categorized_blog()Returns true if a blog has more than 1 category.
- twentysixteen_categorized_blog()Determines whether blog/site has more than one category.
- wp_add_global_styles_for_blocks()Adds global style rules to the inline style for each block.
- wp_ajax_health_check_site_status_result()Handles site health check to update the result status via AJAX.
- wp_dashboard_cached_rss_widget()Checks to see if all of the feed url in $check_urls are cached.
- wp_dashboard_plugins_output()Display plugins text for the WordPress news widget.
- wp_edit_theme_plugin_file()Attempts to edit a file for a theme or plugin.
- wp_maybe_generate_attachment_metadata()Maybe attempts to generate attachment metadata, if missing.
- wp_rand()Generates a random non-negative number.
Source code
function set_transient( $transient, $value, $expiration = 0 ) { $expiration = (int) $expiration; /** * Filters a specific transient before its value is set. * * The dynamic portion of the hook name, `$transient`, refers to the transient name. * * @since 3.0.0 * @since 4.2.0 The `$expiration` parameter was added. * @since 4.4.0 The `$transient` parameter was added. * * @param mixed $value New value of transient. * @param int $expiration Time until expiration in seconds. * @param string $transient Transient name. */ $value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient ); /** * Filters the expiration for a 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 transient. * @param string $transient Transient name. */ $expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient ); if ( wp_using_ext_object_cache() || wp_installing() ) { $result = wp_cache_set( $transient, $value, 'transient', $expiration ); } else { $transient_timeout = '_transient_timeout_' . $transient; $transient_option = '_transient_' . $transient; wp_prime_option_caches( array( $transient_option, $transient_timeout ) ); if ( false === get_option( $transient_option ) ) { $autoload = true; if ( $expiration ) { $autoload = false; add_option( $transient_timeout, time() + $expiration, '', false ); } $result = add_option( $transient_option, $value, '', $autoload ); } else { /* * If expiration is requested, but the transient has no timeout option, * delete, then re-create transient rather than update. */ $update = true; if ( $expiration ) { if ( false === get_option( $transient_timeout ) ) { delete_option( $transient_option ); add_option( $transient_timeout, time() + $expiration, '', false ); $result = add_option( $transient_option, $value, '', false ); $update = false; } else { update_option( $transient_timeout, time() + $expiration ); } } if ( $update ) { $result = update_option( $transient_option, $value ); } } } if ( $result ) { /** * Fires after the value for a specific transient has been set. * * The dynamic portion of the hook name, `$transient`, refers to the transient name. * * @since 3.0.0 * @since 3.6.0 The `$value` and `$expiration` parameters were added. * @since 4.4.0 The `$transient` parameter was added.Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.