从WordPress的Head里删除无用的标记和链接的方法

WordPress会自动在<head>插入很多没用的标记和元素。这些东西包括profile, pingback, alternate, EditURI, wlwmainfest, prev, next, generator meta, shortlink, index, start等。这些标记不仅会对增加页面的体积,而且可能导致安全问题。比如“generator” 元标记就会泄露Wordpress的版本号,如果你没用及时更新一个已经曝出有安全漏洞的版本,那黑客就能根据“generator”提供的版本号判断出你的网站可以用来做实验。所以,下面我们来看看如何去掉它们。

WordPress的Head里没用的标记

下面是拷贝的自动生成的头标记的列表。当然,这些标记并不一定对你来说都是没用的,你可以选择性的保留,下面我会细说。

<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="#">
<link rel="alternate" type="application/rss+xml" title="Comments Feed" href="#" />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="wlwmanifest.xml" />
<link rel='prev' title='#' href='#' />
<link rel='next' title='#' href='#' />
<meta name="generator" content="WordPress 4.1.1" />
<link rel='shortlink' href='#' />
<link rel='index' title='#' />
<link rel='start' title='#' />

虽然很多,但去掉它们并不复杂。我们需要做的只是打开使用的Wordpress主题里的function.php文件,在里面添加几行代码就行了。

去除 XMLRPC, WLW, Generator, Feeds 和 ShortLink

去掉这些标记,向function.php文件里添加下面的代码:

remove_action('wp_head', 'rsd_link'); //removes EditURI/RSD (Really Simple Discovery) link.
remove_action('wp_head', 'wlwmanifest_link'); //removes wlwmanifest (Windows Live Writer) link.
remove_action('wp_head', 'wp_generator'); //removes meta name generator.
remove_action('wp_head', 'wp_shortlink_wp_head'); //removes shortlink.
remove_action( 'wp_head', 'feed_links', 2 ); //removes feed links.
remove_action('wp_head', 'feed_links_extra', 3 );  //removes comments feed.

去除 Previous 和 Next 文章链接

去掉文章的上一篇和下一篇链接,向function.php文件里添加下面的代码:

/*Removes prev and next links*/
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

去除 XFN (XHTML Friends Network) Profile 链接 和 Pingback URL

这个rel=profile链接和rel=Pingback标记可以从header.php文件里直接删除。做法是,打开Wordperss主题里的header.php文件,删除下面两行:

<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">

需要提醒的是,上面要删除的这些链接和标记并不是完全没有用处,否则Wordpress也不会自动生成它们,只是,在大多数情况下,我们是用不着它们的。比如,如果你喜欢用微软的Windows live writer写博客,那么,这个wlwmanifest link 你就必须要保留。所以,去掉它们之前,先考虑一下将来会不会用到它们。

阅读余下内容
 

《“从WordPress的Head里删除无用的标记和链接的方法”》 有 2 条评论

  1. Дорогие друзья!

    Купить диплом в Уфе

    Most themes will display your blog s title and optional tagline either over or next to your header image. In many themes you can change the color of that text from the Customizer using the Colors Backgrounds tool. You can also edit your blog s title and tagline from theCustomizer in the Site Identity panel.

  2. Здравствуйте!

    Купить свидетельство в Кургане

    Changing the header image of the Default WordPress Theme has been simplified with the introduction of a utility called Kubrickr . It simply asks you to supply a new image file name for the header and then switches it for you, so you do not have to dig into the code. If all you want to change is the header image, this is an extremely useful and easy tool.

发表回复

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


京ICP备12002735号