wordpress屏蔽头部大量冗余信息(完全版)

wordpress头部大量不必要的信息不仅会拖慢网页打开速度、降低用户体验、不利于SEO,甚至还存在安全隐患。这样的多余代码多达十几处,全都可以屏蔽、禁用。小撸综合网上众多高手的博客,在此逐条列出禁用它们的原因:

1. 禁用emoji:wordpress调用 http://s.w.org/images/core/emoji/72×72/ 下的图片来渲染 Emoji,而这个地址是国内访问不了的,所以要禁用 WordPress Emoji 功能。

2. 禁止WP自带的jquery.js在头部载入:因为头部载入js会大大影响网页载入速度,非常不利于SEO,有兴趣的读者可以阅读《高性能网站建设指南》一书的第6章。

3. 屏蔽wlwmanifest相关信息:RSD是Really Simple Discoverability简称,一个广义的古老的离线编辑器接口,wlwmanifest为Windows Live Writer准备的。即便需要使用离线编辑器,大部分时候也不需要这两行代码。Live Writer自己知道它们。保留这两行代码可能会留有安全隐患。

4. 禁用feed:一般情况下,订阅用户质量不高,只会占用博客的流量,达不到seo和营销的目的,不要也罢。

5. 屏蔽WP版本信息:因为会被黑客利用,攻击特定版本的WordPress漏洞,最好删掉。

6. 禁用shortlink:shortlink都是带动态的?号的链接。百度在抓取的时候会把该URL也索引了,已经有静态URL,就不要让百度蜘蛛再抓取这个动态链接,所以删掉shortlink更好。

7. 禁用REST信息:WordPress REST API有利于JSON开发。如果不用JSON开发,就没必要用它们了。参考http://www.linuxidc.com/Linux/2017-02/140164.htm,黑客还可以利用REST API内容注入/权限提升漏洞,擅自篡改网站内容。

8. 屏蔽WordPress加载的JS和CSS链接中的版本号:以免版本信息泄露让人抓住把柄。

9. 禁用前后文章信息:WordPress把前后文、第一篇文章和主页链接全放在meta中,对于SEO帮助不大,反使得头部信息巨大。

10. 禁止代码转义为全角字符:Wordpress 会智能地将源代码中的所有半角符号自动修正为全角符号,以防止外部源代码在网页上执行。这个功能所带来的麻烦就是在输入代码后,显示的不是原始代码,被人复制粘贴使用就会有问题。如果你的WordPress博客经常要分享代码的话,就需要彻底解决WordPress中半角全角字符转义的问题。

11. 屏蔽dns-prefetch:添加dns-prefetch,是为了从s.w.org预获取表情和头像,目的是提高网页加载速度 ,但s.w.org国内根本无法访问,预获取和提高速度什么的没用处不说,反而可能会影响速度。

12. 禁用embeds功能、移除wp-embed.min.js文件:wordpress 4.4的embeds功能可以允许更方便的引用第三方资源,oEmbed是一种允许第三方站点上 URL的嵌入式表示的格式……当用户发布一个到某资源的链接时,[它]允许网站显示此嵌入式内容(比如照片或视频),无需直接解析该资源,嵌入第三方视频的时候会用到,没这个需要当然应该屏蔽。

移除wordpress头部大量冗余信息之前的代码,非常非常臃肿:


<head>
 <meta charset="UTF-8" />
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Manjaro是什么? | Manjaro Linux 乐于简单</title>
 <link rel="pingback" href="http://www.manjaro.cn/xmlrpc.php" />
 <link rel="alternate" type="application/rss+xml" title="Manjaro Linux 乐于简单 &raquo; Feed" href="http://www.manjaro.cn/feed" />
 <link rel="alternate" type="application/rss+xml" title="Manjaro Linux 乐于简单 &raquo; 评论Feed" href="http://www.manjaro.cn/comments/feed" />
 <script type="text/javascript">
 window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/localhost\/www.manjaro.cn\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.5.3"}};
 !function(a,b,c){function d(a){var c,d,e,f=b.createElement("canvas"),g=f.getContext&&f.getContext("2d"),h=String.fromCharCode;if(!g||!g.fillText)return!1;switch(g.textBaseline="top",g.font="600 32px Arial",a){case"flag":return g.fillText(h(55356,56806,55356,56826),0,0),f.toDataURL().length>3e3;case"diversity":return g.fillText(h(55356,57221),0,0),c=g.getImageData(16,16,1,1).data,d=c[0]+","+c[1]+","+c[2]+","+c[3],g.fillText(h(55356,57221,55356,57343),0,0),c=g.getImageData(16,16,1,1).data,e=c[0]+","+c[1]+","+c[2]+","+c[3],d!==e;case"simple":return g.fillText(h(55357,56835),0,0),0!==g.getImageData(16,16,1,1).data[0];case"unicode8":return g.fillText(h(55356,57135),0,0),0!==g.getImageData(16,16,1,1).data[0]}return!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i;for(i=Array("simple","flag","unicode8","diversity"),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>
 <link rel='stylesheet' id='bbp-default-css' href='http://www.manjaro.cn/wp-content/plugins/bbpress/templates/default/css/bbpress.css?ver=2.5.12-6148' type='text/css' media='screen' />
 <link rel='stylesheet' id='usp_style-css' href='http://www.manjaro.cn/wp-content/themes/manjarocn/usp/usp.css' type='text/css' media='all' />
 <link rel='stylesheet' id='theme_stylesheet-css' href='http://www.manjaro.cn/wp-content/themes/manjarocn/style.css?ver=4.5.3' type='text/css' media='all' />
 <script type='text/javascript' src='http://www.manjaro.cn/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
 <script type='text/javascript' src='http://www.manjaro.cn/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
 <script type='text/javascript' src='http://www.manjaro.cn/wp-content/plugins/user-submitted-posts/resources/jquery.cookie.js'></script>
 <script type='text/javascript' src='http://www.manjaro.cn/wp-content/plugins/user-submitted-posts/resources/jquery.parsley.min.js'></script>
 <script type='text/javascript'>
 window.ParsleyConfig = { excluded: ".exclude" }; var usp_case_sensitivity = "false"; var usp_challenge_response = "8";
 </script>
 <script type='text/javascript' src='http://www.manjaro.cn/wp-content/plugins/user-submitted-posts/resources/jquery.usp.core.js'></script>
 <link rel='https://api.w.org/' href='http://www.manjaro.cn/wp-json/' />
 <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.manjaro.cn/xmlrpc.php?rsd" />
 <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://www.manjaro.cn/wp-includes/wlwmanifest.xml" />
 <link rel='next' title='用户评价' href='http://www.manjaro.cn/148' />
 <meta name="generator" content="WordPress 4.5.3" />
 <link rel="canonical" href="http://www.manjaro.cn/143" />
 <link rel='shortlink' href='http://www.manjaro.cn/?p=143' />
 <link rel="alternate" type="application/json+oembed" href="http://www.manjaro.cn/wp-json/oembed/1.0/embed?url=http%3A%2F%2Flocalhost%2Fwww.manjaro.cn%2F143" />
 <link rel="alternate" type="text/xml+oembed" href="http://www.manjaro.cn/wp-json/oembed/1.0/embed?url=http%3A%2F%2Flocalhost%2Fwww.manjaro.cn%2F143&#038;format=xml" />
 <link rel="shortcut icon" href="http://www.manjaro.cn/manjarocn.ico" title="Favicon" /></head>

屏蔽wordpress头部大量冗余信息之后的代码,非常精简:


<head>
 <meta charset="UTF-8" />
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Manjaro是什么? | Manjaro Linux 乐于简单</title>
 <link rel="pingback" href="http://www.manjaro.cn/xmlrpc.php" />
 <link rel='stylesheet' id='bbp-default-css' href='http://www.manjaro.cn/wp-content/plugins/bbpress/templates/default/css/bbpress.css' type='text/css' media='screen' />
 <link rel='stylesheet' id='usp_style-css' href='http://www.manjaro.cn/wp-content/themes/manjarocn/usp/usp.css' type='text/css' media='all' />
 <link rel='stylesheet' id='theme_stylesheet-css' href='http://www.manjaro.cn/wp-content/themes/manjarocn/style.css' type='text/css' media='all' />
 <link rel="canonical" href="http://www.manjaro.cn/143" />
 <link rel="shortcut icon" href="http://www.manjaro.cn/manjarocn.ico" title="Favicon" /></head>

干货在此!将以下内容拷贝到主题目录下的functions.php文件里即可:


/*
* WordPress Emoji 的实现方式会调用 http://s.w.org/images/core/emoji/72×72/ 下的图片来渲染 Emoji,而这个地址是国内访问不了的,所以要禁用 WordPress Emoji 功能
*/
function disable_emojis() {
remove_action(‘wp_head’, ‘print_emoji_detection_script’, 7);
remove_action(‘admin_print_scripts’, ‘print_emoji_detection_script’);
remove_action(‘wp_print_styles’, ‘print_emoji_styles’);
remove_action(‘admin_print_styles’, ‘print_emoji_styles’);
remove_filter(‘the_content_feed’, ‘wp_staticize_emoji’);
remove_filter(‘comment_text_rss’, ‘wp_staticize_emoji’);
remove_filter(‘wp_mail’, ‘wp_staticize_emoji_for_email’);
add_filter(‘tiny_mce_plugins’, ‘disable_emojis_tinymce’);
}
add_action(‘init’, ‘disable_emojis’);

/*
* 禁用WP自带的jquery.js,以免在头部载入影响网页载入速度。禁用后,不会影响到wordpress后台效果。
*/
if (!is_admin()) { // 后台不禁止
function my_init_method() {
wp_deregister_script(‘jquery’); // 取消原有的 jquery 定义

}

add_action(‘init’, ‘my_init_method’);
}
/*
* 移除wlwmanifest相关信息。其中RSD是Really Simple Discoverability简称,一个广义的离线编辑器接口,wlwmanifest为Windows Live Writer准备的。如果你不用wlw,可移除之。即便你需要使用离线编辑器,大部分时候也不需要这两行代码。Live Writer自己知道它们。保留这两行代码可能会留有安全隐患。
*/
remove_action(‘wp_head’, ‘rsd_link’);
remove_action(‘wp_head’, ‘wlwmanifest_link’);
/*
*指定博客feed。可以被浏览器检测到,然后被读者订阅。一般情况下,订阅用户质量不高,只会占用博客的流量,达不到seo和营销的目的,不要也罢。
*/
remove_action(‘wp_head’, ‘feed_links’, 2);//文章和评论feed
remove_action(‘wp_head’, ‘feed_links_extra’, 3); //分类等feed
/*
*一篇博文下如有10条评论,则搜索引擎爬虫就会爬10次相同的页面文章,相当于做了多次重复的工作,严重影响了抓取的效率,及耗费了带宽。重复内容造成的结果必然是蜘蛛不愿意来爬,不同的url指向同一个页面,也会影响到该页面的权重。通过canonical标签,能有效的避免这类问题,增加对搜索引擎的友好度,所以这个标签最好保留。
*/
//remove_action(‘wp_head’, ‘rel_canonical’);
/*
*显示WordPress版本信息,可以被黑客利用,攻击特定版本的WordPress漏洞,最好删掉。
*/
remove_action(‘wp_head’, ‘wp_generator’);
/*
*shortlink都是带动态的?号的链接。百度在抓取的时候会把该URL也索引了,已经有静态URL,还让蜘蛛抓取这个动态链接,是不好的,所以删掉shortlink更好。
*/
remove_action(‘wp_head’, ‘wp_shortlink_wp_head’, 10, 0);
remove_action(‘template_redirect’, ‘wp_shortlink_header’, 11, 0);
/*
* WordPress REST API有利于JSON开发。如果不用JSON开发,就没必要用它们了。
*参考http://www.linuxidc.com/Linux/2017-02/140164.htm,黑客还可以利用REST API内容注入/权限提升漏洞,擅自篡改网站内容。
* 所以用下面4行屏蔽rest+json+oembed。
*/
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);
/*
* 移除 WordPress 加载的JS和CSS链接中的版本号,以免版本信息泄露让人抓住把柄。
*/
function wpdaxue_remove_cssjs_ver($src) {
if (strpos($src, ‘ver=’)) {
$src = remove_query_arg(‘ver’, $src);
}
return $src;
}
add_filter(‘style_loader_src’, ‘wpdaxue_remove_cssjs_ver’, 999);
add_filter(‘script_loader_src’, ‘wpdaxue_remove_cssjs_ver’, 999);
/*
* WordPress把前后文、第一篇文章和主页链接全放在meta中,对于SEO帮助不大,反使得头部信息巨大。通过下面4行来移除之。
*/
remove_action(‘wp_head’, ‘index_rel_link’); // Removes the index link
remove_action(‘wp_head’, ‘parent_post_rel_link’, 10, 0); // Removes the prev link
remove_action(‘wp_head’, ‘start_post_rel_link’, 10, 0); // Removes the start link
remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0); // Removes the relational links for the posts adjacent to the current post.
/*
* WordPress中会默认会自动转义一些字符,如将‘–’转义为‘-’破折号,网络上也称“转换全半角标点符号”或者“代码转义”。因为Wordpress 会智能地将源代码中的所有半角符号自动修正为全角符号,以防止外部源代码在网页上执行。这个功能所带来的麻烦就是在输入代码后,显示的不是原始代码,被人复制粘贴使用就会有问题。如果你的WordPress博客经常要分享代码的话,就需要彻底解决WordPress中半角全角字符转义的问题。
*/
remove_filter(‘the_content’, ‘wptexturize’);//取消内容转义
remove_filter(‘the_excerpt’, ‘wptexturize’);//取消摘要转义
remove_filter(‘comment_text’, ‘wptexturize’);//取消评论转义

/*
* WordPress升级到4.6以后,页面源代码的头部会多出一行 <link rel=’dns-prefetch‘ href=’//s.w.org’ />,WordPress 4.6在头部添加dns-prefetch,应该是为了从s.w.org预获取表情和头像,目的是提高网页加载速度 ,但s.w.org国内根本无法访问,预获取和提高速度什么的都是咸吃萝卜淡操心,没用处不说,反而可能会影响速度。
*/
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 );

/*
* 禁用embeds功能、移除wp-embed.min.js文件:wordpress 4.4的embeds功能可以允许更方便的引用第三方资源,oEmbed是一种允许第三方站点上 URL的嵌入式表示的格式……当用户发布一个到某资源的链接时,[它]允许网站显示此嵌入式内容(比如照片或视频),无需直接解析该资源,嵌入第三方视频的时候会用到,没这个需要当然应该屏蔽。
*/
function disable_embeds_init() {
global $wp;
$wp->public_query_vars = array_diff($wp->public_query_vars, array(
’embed’,
));
remove_action(‘rest_api_init’, ‘wp_oembed_register_route’);
add_filter(’embed_oembed_discover’, ‘__return_false’);
remove_filter(‘oembed_dataparse’, ‘wp_filter_oembed_result’, 10);
remove_action(‘wp_head’, ‘wp_oembed_add_discovery_links’);
remove_action(‘wp_head’, ‘wp_oembed_add_host_js’);
add_filter(‘tiny_mce_plugins’, ‘disable_embeds_tiny_mce_plugin’);
add_filter(‘rewrite_rules_array’, ‘disable_embeds_rewrites’);
}

add_action(‘init’, ‘disable_embeds_init’, 9999);

function disable_embeds_tiny_mce_plugin($plugins) {
return array_diff($plugins, array(‘wpembed’));
}

function disable_embeds_rewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, ’embed=true’)) {
unset($rules[$rule]);
}
}
return $rules;
}

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

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

 

https://www.lulinux.com/archives/1884

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注