wordpress必须禁用REST API和移除WP-JSON链接的方法

先说说禁用REST API、移除wp-json链接的方法,将以下代码添加到主题functions.php文件中即可禁用REST API并去除head里面输出的链接信息:

add_filter('rest_enabled', '_return_false');
add_filter('rest_jsonp_enabled', '_return_false');
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

此外,对于 WP 4.7 以上版本,还有种通过rest_api_init这个钩子来禁用 REST API 的方法

/*禁用未登录的用户*/
add_filter( 'rest_api_init', 'lxtx_rest_only_for_authorized_users', 99 );
function lxtx_rest_only_for_authorized_users($wp_rest_server){
    if ( !is_user_logged_in() ) {
        wp_die('非法操作!');
    }
}

 

posted @ 2022-04-29 19:07  xiaobingch  阅读(365)  评论(0)    收藏  举报