delete_site_transient()
云策文档标注
概述
delete_site_transient() 函数用于删除站点瞬态(site transient),适用于多站点网络环境。它根据缓存配置选择不同的删除策略,并触发相关 Hook 以支持扩展操作。
关键要点
- 参数 $transient 为字符串类型,必需,表示瞬态名称,不应进行 SQL 转义。
- 返回布尔值:删除成功返回 true,否则返回 false。
- 函数内部逻辑:如果使用外部对象缓存或处于安装模式,则调用 wp_cache_delete();否则通过 delete_site_option() 删除数据库选项。
- 触发两个 Hook:delete_site_transient_{$transient} 在删除前立即触发,deleted_site_transient 在删除后触发。
- 相关函数包括 wp_installing()、wp_cache_delete()、wp_using_ext_object_cache() 和 delete_site_option()。
- 自 WordPress 2.9.0 版本引入。
代码示例
function delete_site_transient( $transient ) {
do_action( "delete_site_transient_{$transient}", $transient );
if ( wp_using_ext_object_cache() || wp_installing() ) {
$result = wp_cache_delete( $transient, 'site-transient' );
} else {
$option_timeout = '_site_transient_timeout_' . $transient;
$option = '_site_transient_' . $transient;
$result = delete_site_option( $option );
if ( $result ) {
delete_site_option( $option_timeout );
}
}
if ( $result ) {
do_action( 'deleted_site_transient', $transient );
}
return $result;
}
原文内容
Deletes a site transient.
Parameters
$transientstringrequired-
Transient name. Expected to not be SQL-escaped.
Source
function delete_site_transient( $transient ) {
/**
* Fires immediately before a specific site transient is deleted.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
*
* @param string $transient Transient name.
*/
do_action( "delete_site_transient_{$transient}", $transient );
if ( wp_using_ext_object_cache() || wp_installing() ) {
$result = wp_cache_delete( $transient, 'site-transient' );
} else {
$option_timeout = '_site_transient_timeout_' . $transient;
$option = '_site_transient_' . $transient;
$result = delete_site_option( $option );
if ( $result ) {
delete_site_option( $option_timeout );
}
}
if ( $result ) {
/**
* Fires after a transient is deleted.
*
* @since 3.0.0
*
* @param string $transient Deleted transient name.
*/
do_action( 'deleted_site_transient', $transient );
}
return $result;
}
Hooks
- do_action( ‘deleted_site_transient’, string $transient )
-
Fires after a transient is deleted.
- do_action( “delete_site_transient_{$transient}”, string $transient )
-
Fires immediately before a specific site transient is deleted.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |