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

今天想把网站的所有文章的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('您想干嘛?');
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章