添加以下代码到插件文件里,可以实现:
- 已登录用户可以正常使用 wp-json API
- 未登录用户禁止使用 wp-json API
/*
* disable rest api
*/
function disable_rest_api($access)
{
if (is_user_logged_in()) {
return $access;
} else {
return new WP_Error('rest_disabled', __('The REST API on this site has been disabled.'), ['status' => rest_authorization_required_code()]);
}
}
add_filter('rest_authentication_errors', 'disable_rest_api');