今天想把網站的所有文章的url讀取出來,結果發現,很多www.511yj.com/wp-josn/*下的鏈接,原來是wp升級到4.4後的新功能,WordPress 4.4更新新增了REST API功能,通過REST API可以很輕鬆的獲取網站的數據,但是這個功能並不是每個網站都需要的,或者說我需要,但是並不希望他在head裏面輸出,所以給大家介紹下禁用REST API或者說移除head裏面wp-json鏈接的方法。
先說說禁用REST API、移除wp-json鏈接的方法,將以下代碼添加到主題functions.php文件中即可禁用REST API並去除head裏面輸出的鏈接信息:
/** * 禁用REST API、移除wp-json鏈接 * 去除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 ); /** * *禁用未登錄用戶 REST API* * */ add_filter('rest_api_init','rest_only_for_authorized_users',99); function rest_only_for_authorized_users($wp_rest_server){ if(!is_user_logged_in()) { wp_die('您想幹嘛?'); } }