国强极客
有问题请加微信:guoqiang7585
国强极客

wordpress头部优化 去掉rel=canonical rel=shortlink等html标签

wordpress头部优化 去掉rel=canonical rel=shortlink等html标签

我是一个代码精简的偏执狂,看到网站上有多余的用不到的代码,就像去掉,这次发现头部还有 rel=canonical rel=shortlink 等这样没用的代码,那就把他干掉吧。

 

wordpress在默认情况下,头部会出现很多平时用不到的html代码,比如:

代码如下:
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0 – all posts” href=”http://blog.jb51.net/feed” />
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0 – all comments” href=”http://blog.jb51.net/comments/feed” />
<link rel=”pingback” href=”http://blog.jb51.net/xmlrpc.php” />
<link rel=”EditURI” type=”application/rsd+xml” title=”RSD” href=”http://blog.jb51.net/xmlrpc.php?rsd” />
<link rel=”wlwmanifest” type=”application/wlwmanifest+xml” href=”http://blog.jb51.net/wp-includes/wlwmanifest.xml” />
<link rel=’index’ title=’SBM Stone Crusher Machine|Grinding Mill’ href=’http://blog.jb51.net’ />
<meta name=”generator” content=”WordPress 3.4″ />

上面的标签中,比如generator,将会暴露你的博客程序用的是哪个版本,这个信息泄漏有时会对博客的安全产生一定的影响。同时,也并不需要用这么些标签。
去掉一些不用的标签,网上有方法说主题目录下的 functions.php里增加以下代码:

代码如下:
function wpbeginner_remove_version() {
return &rdquo;;
}
add_filter(‘the_generator’, ‘wpbeginner_remove_version’);//wordpress的版本号
remove_action(‘wp_head’, ‘feed_links’, 2);// 包含文章和评论的feed。
remove_action(‘wp_head’, ‘index_rel_link’);//当前文章的索引。
remove_action(‘wp_head’, ‘wlwmanifest_link’); // 外部编辑器如windows live writer必须。
remove_action(‘wp_head’, ‘feed_links_extra’, 3);// 额外的feed,例如category, tag页。
remove_action(‘wp_head’, ‘start_post_rel_link’, 10, 0);// 开始篇
remove_action(‘wp_head’, ‘parent_post_rel_link’, 10, 0);// 父篇
remove_action(‘wp_head’, ‘adjacent_posts_rel_link’, 10, 0); // 上、下篇.

测试了下,可以去掉wordpress的generator,有些代码还是去不掉,但还有另个一个更直接的方法。
就是在wodpress目录下修改wp-includes目录下的default-filters.php,大概在180多行:

代码如下:
//add_action( ‘wp_head’, ‘feed_links’, 2 );
//add_action( ‘wp_head’, ‘feed_links_extra’, 3 );
//add_action( ‘wp_head’, ‘rsd_link’ );
//add_action( ‘wp_head’, ‘wlwmanifest_link’ );
//add_action( ‘wp_head’, ‘index_rel_link’ );
//add_action( ‘wp_head’, ‘parent_post_rel_link’, 10, 0 );
//add_action( ‘wp_head’, ‘start_post_rel_link’, 10, 0 );
//add_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0 );
add_action( ‘wp_head’, ‘locale_stylesheet’ );
add_action( ‘publish_future_post’, ‘check_and_publish_future_post’, 10, 1 );
add_action( ‘wp_head’, ‘noindex’, 1 );
add_action( ‘wp_head’, ‘wp_print_styles’, 8 );
add_action( ‘wp_head’, ‘wp_print_head_scripts’, 9 );
//add_action( ‘wp_head’, ‘wp_generator’ );
//add_action( ‘wp_head’, ‘rel_canonical’ );
add_action( ‘wp_footer’, ‘wp_print_footer_scripts’ );
//add_action( ‘wp_head’, ‘wp_shortlink_wp_head’, 10, 0 );
add_action( ‘template_redirect’, ‘wp_shortlink_header’, 11, 0 );

不需要的直接//注释掉就行了。一般看下就应该明白是什么意思的。这样可以保证html头部不再有其它的代码了。去掉了wordpress的generator标签。

赞赏
对内容有疑问,请加我微信:guoqiang7585
没有标签
首页      CMS      wordpress      wordpress头部优化 去掉rel=canonical rel=shortlink等html标签

国强极客

文章作者

博客站长,有问题请加微信【guoqiang7585】。

国强极客

wordpress头部优化 去掉rel=canonical rel=shortlink等html标签
我是一个代码精简的偏执狂,看到网站上有多余的用不到的代码,就像去掉,这次发现头部还有 rel=canonical rel=shortlink 等这样没用的代码,那就把他干掉吧。   wordpress在默…
扫描二维码继续阅读
2014-06-05