這次開發主題的SEO功能時,需要在wp_head中添加一些自定義代碼,但wp_head函數太討厭的,加載了一大堆亂七八糟的東西,看代碼很不方便,得想辦法刪除掉,這一次的wordpress開發教程就教大家怎么刪除wp_head多余的代碼。
- 代碼來源:CSDN
注意:我這里是為了方便開發主題才這么做的,如果你不知道你在做什么,不要自己添加哦。

在functions.php
的<?php
下添加以下代碼:
/*
* 頭部多余URL清理,放主題(functions.php)
*/
function disable_emojis() {
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代碼
remove_action( 'wp_head', 'wp_print_styles', 8 ); //emoji載入css
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 ); //頭部加載DNS預獲取(dns-prefetch)
}
add_action( 'init', 'disable_emojis' );
//移除WordPress頭部加載DNS預獲取(dns-prefetch)
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
保存后刷新下前臺,看看代碼,是不是干凈了許多。
如果對于這些移除wp_head多余代碼有更多想了解的,可以看看這篇wordpress開發教程哦: