WordPress在账号未登录时,访问wp-json/wp/v2/可能会泄露信息。
解决方法:
在当前主题目录的functions.php文件里添加以下代码:
01 02 03 04 05 06 07 08 09 10 | // 在账号未登录时禁用wp-json/wp/v2/,防止泄露信息 add_filter( 'rest_authentication_errors' , function ( $result ) { if ( ! empty ( $result ) ) { return $result ; } if ( ! is_user_logged_in() ) { return new WP_Error( 'Access denied' , 'You have no permission to handle it.' , array ( 'status' => 401 ) ); } return $result ; }); |