編寫PHP腳本清除WordPress頭部冗余代碼的方法講解
wordpress頭部的代碼非常多,包括WordPress版本,前后文、第一篇文章、主頁meta信息等各種冗余代碼,這些對博主來說是沒有意義的,也對網(wǎng)站的安全有一定的影響,也一度不知道這些代碼是有什么作用、怎么來的和怎么刪除。
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的一個非常重要的函數(shù),基本上所有的主題在header.php這個文件里都會使用到這個函數(shù),而且很多插件為了在header上加 點(diǎn)東西也會用到wp_head(),比如SEO的相關(guān)插件。但是,在wp_head()出現(xiàn)的這個位置,會增加很多并不常用的代碼。可以通過 remove_action移除這些代碼。
remove_action函數(shù)
函數(shù)原型:
remove_action( $tag, $function_to_add, $priority, $accepted_args );
該函數(shù)移除一個附屬于指定動作hook的函數(shù)。該方法可用來移除附屬于特定動作hook的默認(rèn)函數(shù),并可能用其它函數(shù)取而代之。參見remove_filter(), add_action() and add_filter()。
重要:添加hook時的$function_to_remove 和$priority參數(shù)要能夠相匹配,這樣才可以移除hook。該原則也適用于過濾器和動作。移除失敗時不進(jìn)行警告提示。
參數(shù)
- $tag(字符串)(必需)將要被刪除的函數(shù)所連接到的動作hook。默認(rèn)值:None
- $function_to_remove(回調(diào))(必需) 將要被刪除函數(shù)的名稱默認(rèn)值:None
- $priority(整數(shù))(可選)函數(shù)優(yōu)先級(在函數(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自動添加兩行離線編輯器的開放接口
<link rel="EditURI" type="application/rsd+xml" title="RSD" /> <link rel="wlwmanifest" type="application/wlwmanifest+xml" />
其 中RSD是一個廣義的接口,wlwmanifest是針對微軟Live Writer編輯器的。如果你不需要離線編輯,可移除之。即便你需要使用離線編輯器,大部分時候也不需要這兩行代碼。Live Writer自己知道它們。保留這兩行代碼可能會留有安全隱患。清除代碼:
remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' );
移除前后文、第一篇文章、主頁meta信息
WordPress把前后文、第一篇文章和主頁鏈接全放在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)合推出了一個旨在減少重復(fù)內(nèi)容困擾的方法,這對于廣大站長來說不啻是個好事情,不用再擔(dān)心因為網(wǎng)站上有重復(fù)的內(nèi)容而影響到網(wǎng)站頁面的權(quán)重了。
造 成重復(fù)內(nèi)容的原因有很多,最常見的便是多個url地址指向了同一個頁面,比如:wordpress平臺下的一篇日志頁面,包括了文章及評論內(nèi)容。每個評論 都可以有個固定的鏈接地址,,如果有多個評論的話,則每條評論的鏈接都類似于上述格式,只是commentID號有所不同,這些鏈接其實都是指向同一篇文 章的。蜘蛛來爬時,便會依次爬行一遍,這篇文章下如有10條評論,則爬了10次相同的頁面文章,相當(dāng)于做了多次重復(fù)的工作,嚴(yán)重影響了抓取的效率,及耗費(fèi) 了帶寬。
重復(fù)內(nèi)容造成的結(jié)果必然是蜘蛛不愿意來爬,不同的url指向同一個頁面,也會影響到該頁面的權(quán)重。通過canonical標(biāo)簽,能有效的避免這類問題。
需要注意兩點(diǎn):
- 允許指向不同的子域名,不允許指向其他域名
- canonical屬性可以被傳遞
即A頁面聲明B為權(quán)威鏈接,B聲明C為權(quán)威網(wǎng)頁,那么C就是A和B共同的首選權(quán)威版本
如果你的WP版本在2.9之前,需要通過插件(上面已經(jīng)提到)或者手工 Hack 主題的 header.php 文件來使得博客支持。
<link rel="canonical" href="<?php get_permalink()?>" />
在 WordPress 2.9 發(fā)布之后,WordPress 已經(jīng)默認(rèn)支持這一標(biāo)簽了,我們無需做任何動作,主題就支持這一標(biāo)簽。這對于文章固定鏈接的更改很有幫助,可以增加對搜索引擎的友好度。但是如果你覺得這個標(biāo)簽對你無用,也可以移除之:
remove_action( 'wp_head', 'rel_canonical' );
移除feed
HTML 中通過
<link rel="alternate" type="application/rss+xml" title="feed名" />
來指定博客feed??梢员粸g覽器檢測到,然后被讀者訂閱。
如果你不想添加feed,或者想使用燒制的feed(如FeedSky或者Feedburner燒制的feed),可以移除之。
remove_action( 'wp_head', 'feed_links', 2 );//文章和評論feed remove_action( 'wp_head', 'feed_links_extra', 3 ); //分類等feed
如果用的燒制的feed,然后還可以再手動添加feed地址。
- 在CentOS 6 中安裝WordPress(一) 安裝Apache,Mysql, PHP環(huán)境
- Wordpress php 分頁代碼
- wordpress之wp-settings.php
- 如何讓PHPnow支持wordpress靜態(tài)化鏈接的方法
- 調(diào)用WordPress函數(shù)統(tǒng)計文章訪問量及PHP原生計數(shù)器的實現(xiàn)
- 詳解WordPress中用于更新和獲取用戶選項數(shù)據(jù)的PHP函數(shù)
- 解析WordPress中控制用戶登陸和判斷用戶登陸的PHP函數(shù)
- WordPress主題制作中自定義頭部的相關(guān)PHP函數(shù)解析
- CentOS下搭建PHP環(huán)境與WordPress博客程序的全流程總結(jié)
相關(guān)文章
PHP取整函數(shù):ceil,floor,round,intval的區(qū)別詳細(xì)解析
以下是對PHP中的取整函數(shù):ceil,floor,round,intval的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-08-08php基于 swoole 實現(xiàn)的異步處理任務(wù)功能示例
這篇文章主要介紹了php基于 swoole 實現(xiàn)的異步處理任務(wù)功能,涉及swoole的安裝、配置及php調(diào)用swoole守護(hù)進(jìn)程監(jiān)控任務(wù)相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-08-08php stripslashes和addslashes的區(qū)別
這篇文章主要介紹了php stripslashes和addslashes的區(qū)別,特分享下方便需要的朋友。2014-02-02PHP IDE PHPStorm配置支持友好Laravel代碼提示方法
這篇文章主要介紹了PHP IDE PHPStorm配置支持友好Laravel代碼提示方法,重點(diǎn)配置已經(jīng)加紅提示,需要的朋友可以參考下2015-05-05