wordpress頭部的代碼非常多,包括WordPress版本,前后文、第一篇文章、主頁(yè)meta信息等各種冗余代碼,這些對(duì)博主來(lái)說(shuō)是沒(méi)有意義的,也對(duì)網(wǎng)站的安全有一定的影響,也一度不知道這些代碼是有什么作用、怎么來(lái)的和怎么刪除。
wordpress頭部清理代碼如下
將以下代碼插入到你functions.php的文件頭部,除WordPress頭部大量冗余信息
<?php //remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'feed_links_extra', 3 ); 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', 'locale_stylesheet' ); remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 ); //remove_action( 'wp_head', 'noindex', 1 ); //remove_action( 'wp_head', 'wp_print_styles', 8 ); //remove_action( 'wp_head', 'wp_print_head_scripts', 9 ); remove_action( 'wp_head', 'wp_generator' ); //remove_action( 'wp_head', 'rel_canonical' ); remove_action( 'wp_footer', 'wp_print_footer_scripts' ); remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );add_action('widgets_init', 'my_remove_recent_comments_style'); function my_remove_recent_comments_style() { global $wp_widget_factory; remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style')); } ?>
各函數(shù)解釋:
wp_head函數(shù)
wp_head() 是wordpress的一個(gè)非常重要的函數(shù),基本上所有的主題在header.php這個(gè)文件里都會(huì)使用到這個(gè)函數(shù),而且很多插件為了在header上加 點(diǎn)東西也會(huì)用到wp_head(),比如SEO的相關(guān)插件。但是,在wp_head()出現(xiàn)的這個(gè)位置,會(huì)增加很多并不常用的代碼。可以通過(guò) remove_action移除這些代碼。
remove_action函數(shù)
函數(shù)原型:
remove_action( $tag, $function_to_add, $priority, $accepted_args );
該函數(shù)移除一個(gè)附屬于指定動(dòng)作hook的函數(shù)。該方法可用來(lái)移除附屬于特定動(dòng)作hook的默認(rèn)函數(shù),并可能用其它函數(shù)取而代之。參見(jiàn)remove_filter(), add_action() and add_filter()。
重要:添加hook時(shí)的$function_to_remove 和$priority參數(shù)要能夠相匹配,這樣才可以移除hook。該原則也適用于過(guò)濾器和動(dòng)作。移除失敗時(shí)不進(jìn)行警告提示。
參數(shù)
- $tag(字符串)(必需)將要被刪除的函數(shù)所連接到的動(dòng)作hook。默認(rèn)值:None
- $function_to_remove(回調(diào))(必需) 將要被刪除函數(shù)的名稱默認(rèn)值:None
- $priority(整數(shù))(可選)函數(shù)優(yōu)先級(jí)(在函數(shù)最初連接時(shí)定義)默認(rèn)值:10
- $accepted_args(整數(shù))(必需)函數(shù)所接受參數(shù)的數(shù)量。默認(rèn)值:1
返回值
- (布爾值)函數(shù)是否被移除。
- Ttue 函數(shù)被成功移除
- False函數(shù)未被移除
移除WordPress版本
在head區(qū)域,可以看到如下代碼:
<meta name="generator" content="WordPress 3.1.2" />
remove_action( 'wp_head', 'wp_generator' );
移除離線編輯器開放接口
WordPress自動(dòng)添加兩行離線編輯器的開放接口
<link rel="EditURI" type="application/rsd+xml" style="margin: 0px; padding: 0px; line-height: 25.2px; width: 660px; overflow: hidden; clear: both;">remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' );
移除前后文、第一篇文章、主頁(yè)meta信息
WordPress把前后文、第一篇文章和主頁(yè)鏈接全放在meta中。我認(rèn)為于SEO幫助不大,反使得頭部信息巨大。移除代碼:
remove_action( 'wp_head', 'index_rel_link' ); // Removes the index link remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // Removes the prev link remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // Removes the start link remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes the relational links for the posts adjacent to the current post.
移除Canonical標(biāo)記
09年2月份,Google,Yahoo及Microsoft三大搜索引擎聯(lián)合推出了一個(gè)旨在減少重復(fù)內(nèi)容困擾的方法,這對(duì)于廣大站長(zhǎng)來(lái)說(shuō)不啻是個(gè)好事情,不用再擔(dān)心因?yàn)榫W(wǎng)站上有重復(fù)的內(nèi)容而影響到網(wǎng)站頁(yè)面的權(quán)重了。
造 成重復(fù)內(nèi)容的原因有很多,最常見(jiàn)的便是多個(gè)url地址指向了同一個(gè)頁(yè)面,比如:wordpress平臺(tái)下的一篇日志頁(yè)面,包括了文章及評(píng)論內(nèi)容。每個(gè)評(píng)論 都可以有個(gè)固定的鏈接地址,,如果有多個(gè)評(píng)論的話,則每條評(píng)論的鏈接都類似于上述格式,只是commentID號(hào)有所不同,這些鏈接其實(shí)都是指向同一篇文 章的。蜘蛛來(lái)爬時(shí),便會(huì)依次爬行一遍,這篇文章下如有10條評(píng)論,則爬了10次相同的頁(yè)面文章,相當(dāng)于做了多次重復(fù)的工作,嚴(yán)重影響了抓取的效率,及耗費(fèi) 了帶寬。
重復(fù)內(nèi)容造成的結(jié)果必然是蜘蛛不愿意來(lái)爬,不同的url指向同一個(gè)頁(yè)面,也會(huì)影響到該頁(yè)面的權(quán)重。通過(guò)canonical標(biāo)簽,能有效的避免這類問(wèn)題。
需要注意兩點(diǎn):
- 允許指向不同的子域名,不允許指向其他域名
- canonical屬性可以被傳遞
即A頁(yè)面聲明B為權(quán)威鏈接,B聲明C為權(quán)威網(wǎng)頁(yè),那么C就是A和B共同的首選權(quán)威版本
如果你的WP版本在2.9之前,需要通過(guò)插件(上面已經(jīng)提到)或者手工 Hack 主題的 header.php 文件來(lái)使得博客支持。
<link rel="canonical" href="<?php get_permalink()?>" />
在 WordPress 2.9 發(fā)布之后,WordPress 已經(jīng)默認(rèn)支持這一標(biāo)簽了,我們無(wú)需做任何動(dòng)作,主題就支持這一標(biāo)簽。這對(duì)于文章固定鏈接的更改很有幫助,可以增加對(duì)搜索引擎的友好度。但是如果你覺(jué)得這個(gè)標(biāo)簽對(duì)你無(wú)用,也可以移除之:
remove_action( 'wp_head', 'rel_canonical' );
移除feed
HTML 中通過(guò)
<link rel="alternate" type="application/rss+xml" style="margin: 0px; padding: 0px; line-height: 25.2px; width: 660px; overflow: hidden; clear: both;">remove_action( 'wp_head', 'feed_links', 2 );//文章和評(píng)論feed remove_action( 'wp_head', 'feed_links_extra', 3 ); //分類等f(wàn)eed
如果用的燒制的feed,然后還可以再手動(dòng)添加feed地址。