WordPress维护模式(使用插件与不使用插件)
文章作者: Christos Pontikis
发布日期: 2025年5月25日
文章分类: WordPress
标签: hm, how-to, maintenanace mode, php, t_eg, wordpress, wordpress development, wp_dev

当我们需要对WordPress网站进行重大更改(自定义代码和/或数据库更改)时,需要启用WordPress维护模式,这样网站的前端就不会对访问者可用。
不使用插件的WordPress维护模式
将以下代码添加到子主题的functions.php文件中。
如果您没有子主题,可以轻松创建一个。请参阅这篇文章。
使用此代码,网站(管理面板和前端)仅对管理员可用。任何其他用户将看到维护模式消息。
启用WordPress维护模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/**
* Maintenance mode
*/
function wp_maintenance_mode() {
if ( ! current_user_can( 'administrator' ) ) {
wp_logout();
}
wp_die( '<h1>Website under Scheduled Maintenance</h1><br />Please check back soon.' );
}
/**
* Check if current page is the login page
*
* @return bool
*/
function is_wp_login() {
if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
return true;
}
return false;
}
if ( false === is_user_logged_in() && false === is_wp_login() ) {
add_action( 'get_header', 'wp_maintenance_mode' );
} else {
if ( ! current_user_can( 'administrator' ) ) {
add_action( 'admin_init', 'wp_maintenance_mode' );
}
}
|
禁用WordPress维护模式
只需注释掉add_action行(或删除整个if块):
1
2
3
4
5
6
7
|
if ( false === is_user_logged_in() && false === is_wp_login() ) {
//add_action( 'get_header', 'wp_maintenance_mode' );
} else {
if ( ! current_user_can( 'administrator' ) ) {
//add_action( 'admin_init', 'wp_maintenance_mode' );
}
}
|
使用插件的WordPress维护模式
您可以使用许多插件。
在本教程中,我们使用LightStart - Maintenance Mode, Coming Soon and Landing Page Builder
安装并激活插件。
您可能希望使用许多可用选项:
菜单 ⟶ 设置 ⟶ LightStart
但基本设置是:

结果是:

记得清除缓存(如果有),否则插件可能无法正常工作。
Christos Pontikis | 企业家 | 全栈开发人员 | MediSign Ltd.创始人。我在设计和开发Web应用程序方面拥有超过15年的专业经验。在管理(Web)项目方面也非常有经验。
www.pontikis.gr/en