做站的都知道网站需要定期维护,维护期间是不能正常访问的
但如果没有任何提示,可能会流失访客。维护期间做一个公告页面则非常有必要了,当然有插件可实现该功能,界面也挺漂亮。
本文讲的是纯代码实现,且不影响管理员正常访问。先上前端图
这是我的测试网站设置维护模式后前端显示,而同一时间如果是管理员访问则依然可以正常访问
function zhutw_maintenance_mode() {
$current_time = current_time( 'timestamp' );
$end_time ='2025-04-05 22:00:00';
$target_time = strtotime( $end_time );
if ( $current_time > $target_time ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
$style = '
<style>
body {
background-color: #f5f5f5;
text-align: center;
padding: 50px;
}
.buttons-container>.button {
margin: 0 5px;
}
</style>
';
$_QQ = '<svg t="1692542896120" style="margin-bottom: -3px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7104" width="15" height="15"><path d="M173.056 947.712c0 41.984 73.728 76.288 164.352 76.288S501.76 989.696 501.76 947.712s-73.728-76.288-164.352-76.288-164.352 33.792-164.352 76.288zM521.728 947.712c0 41.984 73.728 76.288 164.352 76.288 91.136 0 164.352-34.304 164.352-76.288s-73.728-76.288-164.352-76.288c-90.624-0.512-164.352 33.792-164.352 76.288z" fill="#F5B824" p-id="7105"></path><path d="M878.08 506.88l-36.864-118.784C840.704 1.536 528.384 0 512 0c-16.896 0-328.704 1.536-328.704 388.096L145.92 506.88s-125.44 157.696-40.96 294.912c9.728 6.656 10.24 30.72 61.44-77.312 0 0 68.608 241.664 343.552 243.712h3.072c274.944-2.048 343.552-243.712 343.552-243.712 51.2 108.032 52.224 86.016 61.44 77.312 85.504-137.216-39.936-294.912-39.936-294.912z" fill="#121213" p-id="7106"></path><path d="M526.336 254.464c0 51.712 29.696 93.696 66.56 93.696s66.56-41.984 66.56-93.696c0-51.712-29.696-93.696-66.56-93.696-36.864 0.512-66.56 41.984-66.56 93.696zM355.328 254.464c0 51.712 29.696 93.696 66.56 93.696s66.56-41.984 66.56-93.696c0-51.712-29.696-93.696-66.56-93.696-36.352 0.512-66.56 41.984-66.56 93.696z" fill="#FFFFFF" p-id="7107"></path><path d="M414.72 254.464c0 22.016 12.8 40.448 28.672 40.448 15.872 0 28.672-17.92 28.672-40.448s-12.8-40.448-28.672-40.448c-15.872 0.512-28.672 18.432-28.672 40.448z" fill="#161616" p-id="7108"></path><path d="M781.312 466.944c-113.152 94.208-269.312 83.456-269.312 83.456s-156.672 10.752-269.312-83.456c0 0-107.52 484.864 269.312 484.864s269.312-484.864 269.312-484.864z" fill="#FAFAFB" p-id="7109"></path><path d="M840.704 388.096s-90.112 161.792-328.704 161.792-328.704-161.792-328.704-161.792C144.896 492.032 145.92 506.88 145.92 506.88c36.864 40.96 80.896 74.752 129.536 99.84-3.072 34.816-7.68 88.064-7.168 115.2 0.512 42.496 26.624 45.056 86.528 51.2 59.904 5.632 47.616-10.24 47.616-37.376V650.24c63.488 12.288 109.056 7.68 109.056 7.68s208.896 21.504 366.08-152.064c0.512 1.024 1.536-13.824-36.864-117.76z" fill="#D53118" p-id="7110"></path><path d="M687.616 407.552c-53.76-25.6-140.8-33.792-175.616-33.792s-121.856 8.192-175.616 33.792c-25.6 8.704-35.84 15.36-35.84 24.064 0 1.024 1.024 2.56 2.56 4.096 50.176 31.232 125.44 51.2 209.408 51.2s159.232-19.968 209.408-51.2c1.536-2.048 2.56-3.072 2.56-4.096-0.512-8.704-10.752-15.36-36.864-24.064z" fill="#F0B225" p-id="7111"></path></svg>';
$title = '<h1>网站正在维护中</h1>';
$title1 = '<h2>预计' . $end_time . '结束</h2>';
$subtitle = '<p>对于给您带来的不便,我们深表歉意。请稍后查看,在维护完成之前您可以先加入我们的交流群。</p>';
$buttons = '
<div class="buttons-container">
<a class="button" href="http://qm.qq.com">'. $_QQ .'QQ交流群</a>
</div>
';
wp_die( $style . $title .$title1 . $subtitle . $buttons , '网站正在维护中 - '. get_bloginfo('name') .'' , array( 'response' => 503 ));
}
}
add_action( 'template_redirect', 'zhutw_maintenance_mode' );
该代码改下还能用于闭站备案期间使用,只要设置注册用户或会员能正常访问就行,这里是设置了只有管理员能访问