wp_is_maintenance_mode()
云策文档标注
概述
wp_is_maintenance_mode() 函数用于检查 WordPress 是否处于维护模式。它通过检测根目录下的 .maintenance 文件及其时间戳来判断,并包含过滤器和特殊场景处理。
关键要点
- 函数检查 WordPress 根目录中是否存在 .maintenance 文件,该文件包含 $upgrading 变量,表示文件创建时间。
- 如果文件创建时间在 10 分钟内,则返回 true 表示维护模式启用;否则返回 false。
- 函数包含 apply_filters('enable_maintenance_mode') 钩子,允许插件或非 Web 运行时过滤维护模式启用状态。
- 在抓取致命错误(scraping)时,即使存在 .maintenance 文件,也可能返回 false 以允许请求继续。
- 如果 wp_installing() 返回 true,则直接返回 false,忽略维护模式检查。
代码示例
function wp_is_maintenance_mode() {
global $upgrading;
if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
return false;
}
require ABSPATH . '.maintenance';
if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) {
return false;
}
if ( is_int( $upgrading ) && isset( $_REQUEST['wp_scrape_key'], $_REQUEST['wp_scrape_nonce'] ) ) {
$key = stripslashes( $_REQUEST['wp_scrape_key'] );
$nonce = stripslashes( $_REQUEST['wp_scrape_nonce'] );
if ( md5( $upgrading ) === $key && (int) $nonce === $upgrading ) {
return false;
}
}
if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
return false;
}
return true;
}注意事项
- 维护模式基于 .maintenance 文件存在且时间戳在 10 分钟内,超时后自动失效。
- 使用 enable_maintenance_mode 过滤器可以动态控制维护模式,适用于非 Web 环境或插件定制。
- 在抓取致命错误时,函数有特殊逻辑避免中断错误处理流程。
原文内容
Checks if maintenance mode is enabled.
Description
Checks for a file in the WordPress root directory named “.maintenance”.
This file will contain the variable $upgrading, set to the time the file was created. If the file was created less than 10 minutes ago, WordPress is in maintenance mode.
Source
function wp_is_maintenance_mode() {
global $upgrading;
if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
return false;
}
require ABSPATH . '.maintenance';
// If the $upgrading timestamp is older than 10 minutes, consider maintenance over.
if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) {
return false;
}
// Don't enable maintenance mode while scraping for fatal errors.
if ( is_int( $upgrading ) && isset( $_REQUEST['wp_scrape_key'], $_REQUEST['wp_scrape_nonce'] ) ) {
$key = stripslashes( $_REQUEST['wp_scrape_key'] );
$nonce = stripslashes( $_REQUEST['wp_scrape_nonce'] );
if ( md5( $upgrading ) === $key && (int) $nonce === $upgrading ) {
return false;
}
}
/**
* Filters whether to enable maintenance mode.
*
* This filter runs before it can be used by plugins. It is designed for
* non-web runtimes. If this filter returns true, maintenance mode will be
* active and the request will end. If false, the request will be allowed to
* continue processing even if maintenance mode should be active.
*
* @since 4.6.0
*
* @param bool $enable_checks Whether to enable maintenance mode. Default true.
* @param int $upgrading The timestamp set in the .maintenance file.
*/
if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
return false;
}
return true;
}
Hooks
- apply_filters( ‘enable_maintenance_mode’, bool $enable_checks, int $upgrading )
-
Filters whether to enable maintenance mode.
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |