WordPress 默认会在 header 中输出许多可能用不到的,甚至会产生一些安全问题的东西。而且删除 WordPress 通过 wp_head() 输出的那些用不到的代码,并不会对 WordPress 有不好的影响。下面介绍不用插件删除无用代码的方法,建议根据自己的需要选择删除。

复制需要的代码添加到主题的 functions.php 中即可:

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'wp_head', 'wp_generator' );

以上代码的简单说明:

第一行代码用于删除 header 中与下面类似的代码:(下面代码中的 RSD(Really Simple Discovery)link 主要是一些具有自动发现机制的 XML-RPC 客户端软件才能用到的,可以选择删除。)

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://veatips.com/xmlrpc.php?rsd" />

第二行代码用于删除 header 中与下面类似的代码:(下面代码在使用 Windows Live Writer 编辑日志时才能用到,如不用 Windows Live Writer,可以选择删除。)

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://veatips.com/wp-includes/wlwmanifest.xml" />

第三四五行代码用于删除 header 中与下面类似的代码:(下面代码会在 header 中显示当前日志的上一篇与下一篇日志链接、发布最早的日志链接、首页链接,可以选择删除;其中发布最早的日志链接与首页链接在某些 WordPress 版本中已经默认不再输出显示了,删除代码也就不用添加了。)

<link rel='prev' title='The Previous Post' href='https://veatips.com/previous/' />
<link rel='next' title='The Next Post' href='https://veatips.com/next/' />
<link rel='start' title='First Post' href='https://veatips.com/hello-world/' />
<link rel='index' title='Home Page' href='http://veatips.com' />

第六行代码用于删除 header 中与下面类似的代码:(下面代码会在 header 中显示当前日志的短链接,可以选择删除。)

<link rel='shortlink' href='https://veatips.com/?p=123' />

第七行代码用于删除 header 中与下面类似的代码:(下面代码会在 header 中显示当前 WordPress 的版本号,影响安全,建议删除。)

<meta name="generator" content="WordPress 3.7.1" />

此外,不同版本 WordPress 删除代码可能略有不同,如删除无效,可以打开 wp-includes/default-filters.php,查找 wp_head 即可找到相应的添加代码,据此修改无效的删除代码即可。