【发布时间】:2018-05-14 01:51:06
【问题描述】:
我想在 WordPress rest api 中隐藏端点的定义...在 https://www.wpwhitesecurity.com/wp-json 的情况下,我想返回 404 或空数组,但不返回站点的端点列表。
有什么想法吗?
谢谢!
【问题讨论】:
标签: wordpress rest api hook wordpress-rest-api
我想在 WordPress rest api 中隐藏端点的定义...在 https://www.wpwhitesecurity.com/wp-json 的情况下,我想返回 404 或空数组,但不返回站点的端点列表。
有什么想法吗?
谢谢!
【问题讨论】:
标签: wordpress rest api hook wordpress-rest-api
从 4.4.0 版本开始存在钩子 rest_index,https://developer.wordpress.org/reference/hooks/rest_index/ 中的文档描述:
这包含描述 API 的数据。这包括信息 关于支持的身份验证方案、支持的命名空间、路由 API 上可用,以及有关网站的少量数据。
下一个代码可以按我的需要完美运行:
function my_site_rest_index( $response ){
return array();
}
add_filter('rest_index', 'my_site_rest_index');
【讨论】:
function chuck_disable_rest_endpoints( $access ) {
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_cannot_access', __( 'Only logged users are able to call REST API.', 'disable-json-api' ), array( 'status' => rest_authorization_required_code() ) );
}return $access;
}
add_filter( 'rest_authentication_errors', 'chuck_disable_rest_endpoints' );
这将返回只有登录的用户才能访问 API
【讨论】: