在我們開發WordPress主題時,細心的小伙伴或發現網站頭部如果加載head頁面就會出現很多系統自帶的加載項目,例如自帶的css、js、feed、style等多余信息。

這些加載項目很多是沒有必要加載的,那么我們如何將這些多余的head頭部信息移除呢?
方法很簡單,網上針對此類WordPress優化的教程也很多,今天站長圖庫就給大家整理下。
代碼
//去除頭部多余加載信息
remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本
remove_action( 'wp_head', 'rsd_link' );//移除離線編輯器開放接口
remove_action( 'wp_head', 'wlwmanifest_link' );//移除離線編輯器開放接口
remove_action( 'wp_head', 'index_rel_link' );//去除本頁唯一鏈接信息
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //清除前后文信息
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );//清除前后文信息
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//清除前后文信息
remove_action( 'wp_head', 'feed_links', 2 );//移除文章和評論feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除分類等feed
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //頭部的JS代碼
add_filter( 'show_admin_bar', '__return_false' );//移除wp-json鏈接
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
//remove_action( 'wp_head', 'wp_print_styles', 8 ); //移除后臺插件加載css
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );//移除emoji載入js
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );//emoji載入js
remove_action( 'wp_print_styles', 'print_emoji_styles' );//移除emoji載入css
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action('wp_head','wp_resource_hints',2);//移除dns-prefetch使用方法
將上面代碼添加到主題的functions.php文件中, 保存后在看看網站的代碼。
head頭部是不是少了很多多余加載項信息?






