当前位置:首页 »“秋了秋”个人博客 » SEO优化 » Wordpress3.8去除首页多余冗杂代码

Wordpress3.8去除首页多余冗杂代码

作者:秋了秋 发表时间:2014年01月09日

刚建博客的时候,我们都只会关注网站的成型,慢慢地,网站成型之后,我们就会开始关注seo优化以及一些及其细微的东西,而就仅仅seo就够我们站长折腾,之所以这么说,是因为seo是一个及其细微的东西,精确到以“代码”为单位。

代码构成网页,网页打开速度也是seo优化的一方面,其中网页打开速度主要取决于两大主观因素,其一是服务器的速度,这就涉及到服务器的质量和所在位置;其二就是代码问题,也是我们草根站长唯一力所能及的事。

优化代码不仅仅能提高访客的体验度,同时也是增加对搜索引擎的友好度。大量错误冗杂的代码势必会严重影响网站的速度。

今天无意发现wordpress首页输出了一些额外的无关代码,而这正是我之前不怎么重视的问题,显然这些代码不仅增加了首页的字符数,而且还泄露了博客的一些重要信息,往往黑客就是利用这些漏洞来攻击我们的网站,比如说wordpress版本号,版本越低,漏洞也相对会比较多,而这些漏洞正是黑客用来研究的对象。出于一些原因,并不是每一个站长都会把wordpress更新到最新版,况且更新总会出现一些相应的麻烦事。

现在就来看看wordpress3.8消除冗余代码的一些做法

先来了解有哪些冗余代码:

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="//netblog.cn/b/xmlrpc.php?rsd" />——离线编辑器开放接口

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="//netblog.cn/b/wp-includes/wlwmanifest.xml" />

<link rel="pingback" href="//netblog.cn/b/xmlrpc.php" />——引用pingback

<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="//netblog.cn/feed/atom" />

<meta name="generator" content="WordPress 3.7.1" />——wordpress版本

当你登录的时候还会有以下代码的出现

<link rel='stylesheet' id='admin-bar-css'  href='//netblog.cn/b/wp-includes/css/admin-bar.min.css?ver=3.7.1' type='text/css' media='all' />

<style type="text/css" media="print">#wpadminbar { display:none; }</style>

<style type="text/css" media="screen">

html { margin-top: 28px !important; }

* html body { margin-top: 28px !important; }

</style>

其中版本号代码的移除比较自由,直接在主题的functions文档里的<php  ?>中间插入

function remove_wordpress_version() { return ''; } add_filter('the_generator', 'remove_wordpress_version');

其余的清除代码网上有很多都是相继加入以下代码

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); // 上、下篇.

经过测试是行不通的,至少在wordpress3.8上行不通。

如果加入以上代码都行不通的话,就只有修改

Wodpress的wp-includes目录下的default-filters.php文件(第200行左右)

把不要输出的代码注释掉(在前面加双斜杠//)

// Actions

//add_action( 'wp_head','wp_enqueue_scripts', 1);

//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','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',20);

//add_action( 'wp_head','wp_shortlink_wp_head',10, 0 );

add_action( 'template_redirect',   'wp_shortlink_header',11, 0 );

add_action( 'wp_print_footer_scripts', '_wp_footer_scripts');

add_action( 'init','check_theme_switched',99);

add_action( 'after_switch_theme',  '_wp_sidebars_changed');

这样做虽然能够完全杜绝冗余代码输出,但是很不自由,跟自己的主题是分开的,比如说你的博客搬家时,或者wordpress升级时,你还要重新操作以上注释步骤

0
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/1934.html
目录: SEO优化标签: wordpress,SEO优化,冗余代码 25909次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里