设置/更新站点瞬态的值。
原型
set_site_transient( string $transient, mixed $value, int $expiration )
描述
你不需要序列化值,如果值需要序列化,那么它将在设置之前被序列化。
参考:
- set_transient()
参数
$transient
(string)
(Required)
瞬态名称。预计不会被SQL转义。长度不得超过167个字符。
$value
(mixed)
(Required)
瞬态值。预计不会被SQL转义。
$expiration
(int)
(Optional)
到期的时间,以秒为单位。默认为0(无到期)。
返回值
(bool)
如果未设置值,则返回false,如果设置了值,则返回true。
源文件
路径:wp-includes/option.php
<?php
...
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() ) {
$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
} else {
$transient_timeout = '_site_transient_timeout_' . $transient;
$option = '_site_transient_' . $transient;
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 3.0.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( 'setted_site_transient', $transient, $value, $expiration );
}
return $result;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/set_site_transient/