使用方法

添加到wp-config.php顶部

配置参数

define('MAINTENANCE_MODE', true);           // 启用维护模式
define('MAINTENANCE_UNTIL', '2025-11-25 23:00:00');  // 维护结束时间
define('MAINTENANCE_ALLOW_IPS', '');        // IP白名单,逗号分隔

代码

中文版

示例:https://10.1pxeye.com/docs/wp-maintenance/cn.php

define('MAINTENANCE_MODE', true);
define('MAINTENANCE_UNTIL', '2025-11-25 23:00:00');
define('MAINTENANCE_ALLOW_IPS', '');

function get_real_ip(){
    $keys=['HTTP_CF_CONNECTING_IP','HTTP_X_REAL_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR'];
    foreach($keys as $k){
        if(!empty($_SERVER[$k])){
            if($k==='HTTP_X_FORWARDED_FOR'){
                foreach(array_map('trim',explode(',',$_SERVER[$k])) as $ip){
                    if(filter_var($ip,FILTER_VALIDATE_IP)) return $ip;
                }
            } else {
                $ip=trim(explode(',',$_SERVER[$k])[0]);
                if(filter_var($ip,FILTER_VALIDATE_IP)) return $ip;
            }
        }
    }
    return $_SERVER['REMOTE_ADDR']??'0.0.0.0';
}

$is_admin_ip = in_array(get_real_ip(), array_filter(array_map('trim', explode(',', MAINTENANCE_ALLOW_IPS))));
$is_wordpress_upgrading = (defined('ABSPATH') && file_exists(ABSPATH . '.maintenance')) || defined('WP_INSTALLING');

if((MAINTENANCE_MODE || $is_wordpress_upgrading) && !$is_admin_ip){
    http_response_code(503);
    header('Retry-After: 1800');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');
    $site_name = htmlspecialchars($_SERVER['HTTP_HOST']??'网站', ENT_QUOTES, 'UTF-8');
    $date = (new DateTime(MAINTENANCE_UNTIL, new DateTimeZone('Asia/Shanghai')))->format('Y年m月d日 H:i');
    exit("<!DOCTYPE html><html lang=\"zh-CN\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"><meta http-equiv=\"refresh\" content=\"600\"><title>维护中 - {$site_name}</title><style>*{margin:0;padding:0;box-sizing:border-box}html{font-size:14px}body{font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,\"Helvetica Neue\",Arial,\"PingFang SC\",\"Microsoft Yahei\",sans-serif;line-height:1.6;min-height:100vh;display:flex;align-items:center;justify-content:center;flex-direction:column;padding:20px;text-align:center;background:linear-gradient(135deg,#1e3c72,#2a5298);color:#fff;gap:1rem}h1{font-size:2rem;line-height:1.2}p{font-size:1rem}.icon{font-size:3rem}.message{font-size:1.2rem}small{display:block;font-size:.8em}</style></head><body><div class=\"icon\">🚧</div><h1>网站维护中</h1><p class=\"message\">网站正在进行升级维护,暂时无法访问</p><p><small>预计恢复时间:{$date}<br>如有紧急问题,请联系:admin@example.com</small></p></body></html>");
}

英文版

示例:https://10.1pxeye.com/docs/wp-maintenance/en.php

define('MAINTENANCE_MODE', true);
define('MAINTENANCE_UNTIL', '2025-11-25 23:00:00');
define('MAINTENANCE_ALLOW_IPS', '');

function get_real_ip(){
    $keys=['HTTP_CF_CONNECTING_IP','HTTP_X_REAL_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR'];
    foreach($keys as $k){
        if(!empty($_SERVER[$k])){
            if($k==='HTTP_X_FORWARDED_FOR'){
                foreach(array_map('trim',explode(',',$_SERVER[$k])) as $ip){
                    if(filter_var($ip,FILTER_VALIDATE_IP)) return $ip;
                }
            } else {
                $ip=trim(explode(',',$_SERVER[$k])[0]);
                if(filter_var($ip,FILTER_VALIDATE_IP)) return $ip;
            }
        }
    }
    return $_SERVER['REMOTE_ADDR']??'0.0.0.0';
}

$is_admin_ip = in_array(get_real_ip(), array_filter(array_map('trim', explode(',', MAINTENANCE_ALLOW_IPS))));
$is_wordpress_upgrading = (defined('ABSPATH') && file_exists(ABSPATH . '.maintenance')) || defined('WP_INSTALLING');

if((MAINTENANCE_MODE || $is_wordpress_upgrading) && !$is_admin_ip){
    http_response_code(503);
    header('Retry-After: 1800');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');
    $site_name = htmlspecialchars($_SERVER['HTTP_HOST']??'Website', ENT_QUOTES, 'UTF-8');
    $date = (new DateTime(MAINTENANCE_UNTIL, new DateTimeZone('Asia/Shanghai')))->format('F j, Y g:i A') . ' (GMT+8)';
    exit("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"><meta http-equiv=\"refresh\" content=\"600\"><title>Maintenance - {$site_name}</title><style>*{margin:0;padding:0;box-sizing:border-box}html{font-size:14px}body{font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,\"Helvetica Neue\",Arial,sans-serif;line-height:1.6;min-height:100vh;display:flex;align-items:center;justify-content:center;flex-direction:column;padding:20px;text-align:center;background:linear-gradient(135deg,#1e3c72,#2a5298);color:#fff;gap:1rem}h1{font-size:2rem;line-height:1.2}p{font-size:1rem}.icon{font-size:3rem}.message{font-size:1.2rem}small{display:block;font-size:.8em}</style></head><body><div class=\"icon\">🚧</div><h1>Under Maintenance</h1><p class=\"message\">Our website is currently undergoing scheduled maintenance</p><p><small>Expected completion: {$date}<br>For urgent inquiries, please contact: admin@example.com</small></p></body></html>");
}

日文版

示例:https://10.1pxeye.com/docs/wp-maintenance/ja.php

<?php
define('MAINTENANCE_MODE', true);
define('MAINTENANCE_UNTIL', '2025-11-25 23:00:00');
define('MAINTENANCE_ALLOW_IPS', '');

function get_real_ip()
{
    $keys = ['HTTP_CF_CONNECTING_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'];
    foreach ($keys as $k) {
        if (!empty($_SERVER[$k])) {
            if ($k === 'HTTP_X_FORWARDED_FOR') {
                foreach (array_map('trim', explode(',', $_SERVER[$k])) as $ip) {
                    if (filter_var($ip, FILTER_VALIDATE_IP))
                        return $ip;
                }
            } else {
                $ip = trim(explode(',', $_SERVER[$k])[0]);
                if (filter_var($ip, FILTER_VALIDATE_IP))
                    return $ip;
            }
        }
    }
    return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
}

$is_admin_ip = in_array(get_real_ip(), array_filter(array_map('trim', explode(',', MAINTENANCE_ALLOW_IPS))));
$is_wordpress_upgrading = (defined('ABSPATH') && file_exists(ABSPATH . '.maintenance')) || defined('WP_INSTALLING');

if ((MAINTENANCE_MODE || $is_wordpress_upgrading) && !$is_admin_ip) {
    http_response_code(503);
    header('Retry-After: 1800');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');
    $site_name = htmlspecialchars($_SERVER['HTTP_HOST'] ?? 'ウェブサイト', ENT_QUOTES, 'UTF-8');
    $date = (new DateTime(MAINTENANCE_UNTIL, new DateTimeZone('Asia/Tokyo')))->format('Y年n月j日 H:i');
    exit("<!DOCTYPE html><html lang=\"ja\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"><meta http-equiv=\"refresh\" content=\"600\"><title>メンテナンス中 - {$site_name}</title><style>*{margin:0;padding:0;box-sizing:border-box}html{font-size:14px}body{font-family:\"游明朝\",\"Yu Mincho\",YuMincho,\"Hiragino Mincho ProN\",\"Hiragino Mincho Pro\",\"メイリオ\",Meiryo,\"MS P明朝\",\"MS PMincho\",serif;line-height:1.6;min-height:100vh;display:flex;align-items:center;justify-content:center;flex-direction:column;padding:20px;text-align:center;background:linear-gradient(135deg,#1e3c72,#2a5298);color:#fff;gap:1rem}h1{font-size:2rem;line-height:1.2}p{font-size:1rem}.icon{font-size:3rem}.message{font-size:1.2rem}small{line-height:1.8;display:block;font-size:.8em}</style></head><body><div class=\"icon\">🚧</div><h1>メンテナンス中</h1><p class=\"message\">ただいま、システムメンテナンスを行っております。<br>ご不便をおかけいたしますが、今しばらくお待ちください。</p><p><small>【再開予定時刻】{$date}<br>【緊急のご用件はこちらまで】admin@example.com</small></p></body></html>");
}

补充说明

白名单示例

define('MAINTENANCE_ALLOW_IPS', '192.168.1.1,10.0.0.1,203.0.113.0');

核心功能与特点:

  1. 智能IP白名单,兼顾开发与体验:通过get_real_ip()函数,在维护期间,你可以将管理员的IP加入白名单,使其能正常访问网站进行测试或内容更新,而普通访客则看到维护页面。这对于真实开发场景至关重要。
  2. 无缝集成WordPress:代码会自动检测WordPress核心升级状态(ABSPATH . '.maintenance'文件)。即使你没手动开启维护,当WordPress自动更新时,此脚本也会自动生效,避免了用户在升级期间访问网站出现数据库错误等混乱情况。
  3. 对SEO和用户友好:返回了正确的503(服务不可用)状态码和Retry-After头部。这明确告知搜索引擎“网站只是暂时维护,请稍后再试”,能很好地保护网站权重。同时,生成的HTML页面简洁美观,信息清晰,包含预计恢复时间,提升了用户体验。