// 添加到主题的functions.php文件
add_action(‘init’, ‘restrict_chinese_access’);
function restrict_chinese_access() {
// 跳过管理员后台
if (is_admin()) {
// 获取访客IP
$visitor_ip = $_SERVER[‘REMOTE_ADDR’];
// 江苏IP段示例(需要替换为实际江苏IP段)
$jiangsu_ips = [
‘58.208.0.0-58.223.255.255’,
‘114.212.0.0-114.215.255.255’,
// 添加更多江苏IP段
];
$allowed = false;
foreach ($jiangsu_ips as $ip_range) {
if (ip_in_range($visitor_ip, $ip_range)) {
$allowed = true;
break;
}
}
// 非江苏IP禁止访问后台
if (!$allowed) {
wp_die(‘Access Denied’, 403);
}
return;
}
// 检测中文浏览器
$is_chinese_browser = false;
if (isset($_SERVER[‘HTTP_ACCEPT_LANGUAGE’])) {
$lang = strtolower($_SERVER[‘HTTP_ACCEPT_LANGUAGE’]);
if (strpos($lang, ‘zh’) !== false || strpos($lang, ‘cn’) !== false) {
$is_chinese_browser = true;
}
}
// 检测VPN(通过常见VPN头信息)
$is_vpn = false;
$vpn_headers = [‘HTTP_X_FORWARDED_FOR’, ‘HTTP_CLIENT_IP’, ‘HTTP_VIA’, ‘HTTP_PROXY_CONNECTION’];
foreach ($vpn_headers as $header) {
if (!empty($_SERVER[$header])) {
$is_vpn = true;
break;
}
}
// 检测中国IP(需要实际的中国IP数据库或服务)
$is_chinese_ip = false;
$visitor_ip = $_SERVER[‘REMOTE_ADDR’];
// 示例:使用IP数据库或API(需自行实现)
// $is_chinese_ip = check_chinese_ip($visitor_ip);
// 如果是VPS登录(检测SSH或特定端口,简化为IP检测)
$is_vps = false; // 需要具体VPS检测逻辑
// 如果满足任一条件,显示建设中页面
if ($is_chinese_browser || $is_vpn || $is_chinese_ip || $is_vps) {
// 加载建设中模板
include(get_template_directory() . ‘/suspended-construction.php’);
exit;
}
}
// IP范围检测函数
function ip_in_range($ip, $range) {
if (strpos($range, ‘-‘) === false) {
return $ip === $range;
}
list($start_ip, $end_ip) = explode(‘-‘, $range);
$start = ip2long(trim($start_ip));
$end = ip2long(trim($end_ip));
$ip = ip2long($ip);
return ($ip >= $start && $ip <= $end);
}