WordPress 禁用REST API功能

在现有主题文件夹中的functions.php文件结尾或自定义插件中添加以下代码即可:

/*
 * 屏蔽 REST API
 */
add_filter('json_enabled', '__return_false');
add_filter('json_jsonp_enabled', '__return_false');
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
// 移除头部wp-json标签与HTTP header中的link
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11);
add_filter('rest_authentication_errors', 'demo_disable_rest_api');
function demo_disable_rest_api($access)
{
    return new WP_Error('rest_disabled', __('The REST API on this site has been disabled.'), ['status' => rest_authorization_required_code()]);
}

 

转载于:https://www.cnblogs.com/kuguats/articles/8303042.html

你可能感兴趣的:(WordPress 禁用REST API功能)