隐藏 WordPress Rest API 端点定义

Hide WordPress Rest API endpoints definition

我想在 WordPress rest 中隐藏端点的定义 api... 在 https://www.wpwhitesecurity.com/wp-json 的情况下,我想 return 一个 404 或一个空数组,但是不是站点的端点列表。

有什么想法吗?

谢谢!

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' );

这将 return 只有登录用户才能访问 API

从版本 4.4.0 开始存在钩子 rest_index,https://developer.wordpress.org/reference/hooks/rest_index/ 中的文档描述:

This contains the data describing the API. This includes information about supported authentication schemes, supported namespaces, routes available on the API, and a small amount of data about the site.

下一个代码完全符合我的需要:

function my_site_rest_index( $response ){
    return array();
}
add_filter('rest_index', 'my_site_rest_index');