wordpress建站的网页头部有一些不常用的冗余信息,只需要在主题的function.php文件中加入以下代码:
remove_action( 'wp_head', 'wp_generator' );
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', 'rel_canonical' );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
添加了什么?
remove_action函数
顾名思义,按照翻译过来的中文,它就是一个移除指定动作的函数,明白这些就足够了。
remove_action( ‘wp_head’, ‘wp_generator’ );
移除WordPress版本:Chrome 浏览器里输入你的网址,打开后随便在哪里鼠标右键 > 审查元素,在head区域,是否看到如下代码:
这是WordPress版本信息,默认添加。虽然是隐形显示,但是也可以被黑客利用,攻击特定版本的WordPress漏洞。
remove_action( ‘wp_head’, ‘rsd_link’ );
remove_action( ‘wp_head’, ‘wlwmanifest_link’ );
移除离线编辑器开放接口:WordPress自动添加两行离线编辑器的开放接口,审查元素后head区域你应该会看到
其 中RSD是一个广义的接口,wlwmanifest是针对微软Live Writer编辑器的。如果你不需要离线编辑,可移除之。即便你需要使用离线编辑器,大部分时候也不需要这两行代码,而且可能会留有安全隐患。
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 );
移除首页链接、前后文链接、第一篇文章链接和相邻文章链接:WordPress把这些链接全放在meta中。我认为于SEO帮助不大,反使得头部信息巨大。我检查了我的head区域,没有这些
remove_action( ‘wp_head’, ‘rel_canonical’ );
移除Canonical标记:网站上有重复的内容会影响到网站页面的权重,造成重复内容的原因有很多,最常见的便是多个url地址指向了同一个页面,通过canonical标签,能有效的避免这类问题。如果你觉得这个标签对你无用,就可以和我一样移除之,当然不想移除就删除这行代码即可。
remove_action( ‘wp_head’, ‘feed_links’, 2 );
remove_action( ‘wp_head’, ‘feed_links_extra’, 3 );
移除文章和评论Feed :通过head区域的来指定博客feed。可以被浏览器检测到,然后被读者订阅。但是如果你用的烧制的feed(FeedSky或者Feedburner烧制的feed),你就可以使用上述代码移除之。