WordPress移除头部不必要的元素标签

 

我们在制作wp主题的过程中免不了使用wp_head()函数,这个函数会自动加载我们可能用不到的wp版本号、css、js、meta元素和标签,所以我们可以通过移除这些不必要的元素来优化我们的wp头部。

1、移除WordPress版本信息
WordPress自动添加版本号信息,在head区域,可以看到

<meta name=”generator” content=”WordPress 4.8″ />

版本号是默认添加的,但是可以被黑客利用,攻击特定版本的WordPress漏洞。清除代码:

remove_action( ‘wp_head’, ‘wp_generator’ );

2、移除离线编辑器开放接口
WordPress自动添加两行离线编辑器的开放接口,在head区域,可以看到

<link rel=”EditURI” type=”application/rsd+xml” title=”RSD” href=”http://wordpress.cc/xmlrpc.php?rsd” />
<link rel=”wlwmanifest” type=”application/wlwmanifest+xml” href=”http://wordpress.cc/wp-includes/wlwmanifest.xml” />

其中RSD是一个广义的接口,wlwmanifest是针对微软Live Writer编辑器的。如果你不需要离线编辑,可移除之。即便你需要使用离线编辑器,大部分时候也不需要这两行代码。Live Writer自己知道它们。保留这两行代码可能会留有安全隐患。清除代码:

remove_action( ‘wp_head’, ‘rsd_link’ );
remove_action( ‘wp_head’, ‘wlwmanifest_link’ );

3、移除前后文、第一篇文章、主页meta信息
WordPress把前后文、第一篇文章和主页链接全放在meta中。我认为于SEO帮助不大,反使得头部信息巨大。移除代码:

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 );

4、移除feed
HTML中通过来指定博客feed。可以被浏览器检测到,然后被读者订阅。如果你不想添加feed,或者想使用烧制的feed(如FeedSky或者Feedburner烧制的feed),可以移除之。

remove_action( ‘wp_head’, ‘feed_links’, 2 );//文章和评论feed
remove_action( ‘wp_head’, ‘feed_links_extra’, 3 ); //分类等feed

5、禁用embeds功能并移除wp-embed.min.js文件
WordPress自动添加embeds功能和wp-embed.min.js文件,在head区域,可以看到

<script type=’text/javascript’ src=’http://wordpress.cc/wp-includes/js/wp-embed.min.js?ver=4.8′></script>

移除代码

function disable_embeds_init() {
/* @var WP $wp */
global $wp;

// Remove the embed query var.
$wp->public_query_vars = array_diff( $wp->public_query_vars, array(
’embed’,
) );

// Remove the REST API endpoint.
remove_action( ‘rest_api_init’, ‘wp_oembed_register_route’ );

// Turn off
add_filter( ’embed_oembed_discover’, ‘__return_false’ );

// Don’t filter oEmbed results.
remove_filter( ‘oembed_dataparse’, ‘wp_filter_oembed_result’, 10 );

// Remove oEmbed discovery links.
remove_action( ‘wp_head’, ‘wp_oembed_add_discovery_links’ );

// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action( ‘wp_head’, ‘wp_oembed_add_host_js’ );
add_filter( ‘tiny_mce_plugins’, ‘disable_embeds_tiny_mce_plugin’ );

// Remove all embeds rewrite rules.
add_filter( ‘rewrite_rules_array’, ‘disable_embeds_rewrites’ );
}
add_action( ‘init’, ‘disable_embeds_init’, 9999 );
/**
* Removes the ‘wpembed’ TinyMCE plugin.
*
* @since 1.0.0
*
* @param array $plugins List of TinyMCE plugins.
* @return array The modified list.
*/
function disable_embeds_tiny_mce_plugin( $plugins ) {
return array_diff( $plugins, array( ‘wpembed’ ) );
}
/**
* Remove all rewrite rules related to embeds.
*
* @since 1.2.0
*
* @param array $rules WordPress rewrite rules.
* @return array Rewrite rules without embeds rules.
*/
function disable_embeds_rewrites( $rules ) {
foreach ( $rules as $rule => $rewrite ) {
if ( false !== strpos( $rewrite, ’embed=true’ ) ) {
unset( $rules[ $rule ] );
}
}

return $rules;
}
/**
* Remove embeds rewrite rules on plugin activation.
*
* @since 1.2.0
*/
function disable_embeds_remove_rewrite_rules() {
add_filter( ‘rewrite_rules_array’, ‘disable_embeds_rewrites’ );
flush_rewrite_rules();
}
register_activation_hook( __FILE__, ‘disable_embeds_remove_rewrite_rules’ );

/**
* Flush rewrite rules on plugin deactivation.
*
* @since 1.2.0
*/
function disable_embeds_flush_rewrite_rules() {
remove_filter( ‘rewrite_rules_array’, ‘disable_embeds_rewrites’ );
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, ‘disable_embeds_flush_rewrite_rules’ );

 

6、移除WordPress头部加载DNS预获取(dns-prefetch)
在head我们可以看到

<link rel=’dns-prefetch’ href=’//s.w.org’ />

1

移除代码

function remove_dns_prefetch( $hints, $relation_type ) {
if ( ‘dns-prefetch’ === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}

return $hints;
}
add_filter( ‘wp_resource_hints’, ‘remove_dns_prefetch’, 10, 2 );

 

7、移除emoji表情script和style
在head我门可以看到

<script type=”text/javascript”>
window._wpemojiSettings = {“baseUrl”:”https:\/\/s.w.org\/images\/core\/emoji\/2.3\/72×72\/”,”ext”:”.png”,”svgUrl”:”https:\/\/s.w.org\/images\/core\/emoji\/2.3\/svg\/”,”svgExt”:”.svg”,”source”:{“concatemoji”:”http:\/\/wordpress.cc\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.8″}};
!function(a,b,c){function d(a){var b,c,d,e,f=String.fromCharCode;if(!k||!k.fillText)return!1;switch(k.clearRect(0,0,j.width,j.height),k.textBaseline=”top”,k.font=”600 32px Arial”,a){case”flag”:return k.fillText(f(55356,56826,55356,56819),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,56826,8203,55356,56819),0,0),c=j.toDataURL(),b===c&&(k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447),0,0),c=j.toDataURL(),b!==c);case”emoji4″:return k.fillText(f(55358,56794,8205,9794,65039),0,0),d=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55358,56794,8203,9794,65039),0,0),e=j.toDataURL(),d!==e}return!1}function e(a){var c=b.createElement(“script”);c.src=a,c.defer=c.type=”text/javascript”,b.getElementsByTagName(“head”)[0].appendChild(c)}var f,g,h,i,j=b.createElement(“canvas”),k=j.getContext&&j.getContext(“2d”);for(i=Array(“flag”,”emoji4″),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],”flag”!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener(“DOMContentLoaded”,g,!1),a.addEventListener(“load”,g,!1)):(a.attachEvent(“onload”,g),b.attachEvent(“onreadystatechange”,function(){“complete”===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
</script>
<style type=”text/css”>
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>

 

移除代码

remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7 );
remove_action( ‘wp_print_styles’, ‘print_emoji_styles’ );

1
2

8、移除wp-json链接
在head我们可以看到

<link rel=’https://api.w.org/’ href=’http://wordpress.cc/wp-json/’ />

1

移除代码

remove_action( ‘wp_head’, ‘rest_output_link_wp_head’, 10 );

————————————————
版权声明:本文为CSDN博主「PeakXin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xinyflove/article/details/76760922

发表评论

后才能评论