// 拦截所有跳转 add_action('send_headers', function() { $headers = headers_list(); foreach ($headers as $h) { if (stripos($h, 'location:') !== false) { $url = trim(substr($h, strpos($h, ':') + 1)); $host = parse_url($url, PHP_URL_HOST); if ($host && $host !== $_SERVER['HTTP_HOST'] && $host !== parse_url(home_url(), PHP_URL_HOST)) { header_remove('Location'); wp_die('跳转已拦截: ' . $url); } } } }, 1); // 过滤 wp_redirect add_filter('wp_redirect', function($location, $status) { $allowed = [ home_url(), site_url(), wp_login_url(), wp_logout_url(), admin_url(), ]; foreach ($allowed as $a) { if (strpos($location, $a) === 0) return $location; } return home_url(); // 全部重定向回首页 }, 10, 2);
