定期维护&计时结束维护模式,方便解决好用-建站综合交流论坛-网站建设-站长窝

定期维护&计时结束维护模式,方便解决好用

代码介绍

下边代码是很基础的功能,我正好看到子比很多用户需要这个功能,然后我也正好需要我就随便写了个,这个主要功能是比如自己的网站想要维护一天,并且自己管理员账号还需要可以访问,然后还想要对网站没任何影响,这样你就需要了下边的代码,下边的代码也就是开启了定期维护2023-08-20 23:00:00这个时间是结束维护时间代码的意思也就是说如果当前时间大于结束维护时间则不在执行往下的代码,如果小于则执行下边的维护代码,维护代码我使用了 current_user_can( ‘manage_options’ ) 这个是判断用户身份是否为管理员,我在这个之前加了个叹号,作用就是如果不是管理员则执行维护模式,我使用了wordpress自带的wp_die函数,分别传递了三个参数分别为
1.提醒内容

2.网页的title标题

3.网页的相应状态码

详细代码

function zhutw_maintenance_mode() {
    $current_time = current_time( 'timestamp' );
    $target_time = strtotime( '2023-08-20 23:00:00' );

    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>
        ';
        $_Telegram = '<svg t="1692542667001" style="margin-bottom: -3px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4029" width="15" height="15"><path d="M512 512m-464 0a464 464 0 1 0 928 0 464 464 0 1 0-928 0Z" p-id="4030" data-spm-anchor-id="a313x.search_index.0.i0.4d8f3a81byJwZH" class="selected" fill="#1296db"></path><path d="M750.56 336l-77.28 364.8c-5.76 25.76-20.96 32-42.56 20l-117.76-86.72L456.16 688a29.76 29.76 0 0 1-23.68 11.52l8.48-119.84L659.04 384c9.6-8.48-1.92-13.12-14.72-4.8L374.56 548.32 258.56 512c-25.28-8-25.76-25.28 5.12-37.44l454.24-175.04c21.12-7.84 39.52 4.48 32.64 36.48z" fill="#FFFFFF" p-id="4031"></path></svg>';
        $_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>';
        $subtitle = '<p>对于给您带来的不便,我们深表歉意。请稍后查看,在维护完成之前您可以先加入我们的交流群。</p>';
        $buttons = '
            <div class="buttons-container">
                <a class="button" href="QQ群链接">'. $_QQ .'QQ交流群</a>
                <a class="button" href="链接">'. $_Telegram .' TG交流群</a>
            </div>
        ';
        wp_die( $style . $title . $subtitle . $buttons , '网站正在维护中 - '. get_bloginfo('name') .'' , array( 'response' => 503 ));
    }
}
add_action( 'template_redirect', 'zhutw_maintenance_mode' );

使用方法

可以自主选择是加在子比主题下的functions.php也可以自己创建func.php文件然后添加进去即可。最后在悄悄地告诉你这个方法不仅仅适用于子比主题模板哦。

演示图

IMG_0767

 

请登录后发表评论

    没有回复内容