/** * REST API 前台全局拦截(404伪装版) * 仅允许WP后台内部请求,游客+登录会员全部禁止访问API * 适用场景:前台关闭搜索下拉、滚动加载等依赖REST的AJAX功能 */ add_filter('rest_authentication_errors', 'disable_all_front_rest_api_404'); function disable_all_front_rest_api_404($access) { // 后台请求直接放行,保障后台功能正常 if ( is_admin() ) { return $access; } // 前台所有API访问,返回404伪装接口不存在 return new WP_Error( 'rest_not_found', 'Not Found', array('status' => 404) ); } // 移除页面中REST API发现链接,防止爬虫探测接口地址 remove_action('wp_head', 'rest_output_link_wp_head'); remove_action('wp_head', 'wp_oembed_add_discovery_links'); remove_action('template_redirect', 'rest_output_link_header', 11); // 禁用WordPress原生wp-sitemap.xml站点地图 add_filter( 'wp_sitemaps_enabled', '__return_false' ); /** * 保护后台会员登录 */ add_action('login_enqueue_scripts','login_protection_new'); function login_protection_new(){ $access_key = isset($_GET['zm']) ? sanitize_text_field($_GET['zm']) : ''; if ($access_key !== 'xz') { status_header(404); nocache_headers(); include get_404_template(); exit; } }