溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何編寫(xiě)PHP腳本清除WordPress頭部冗余代碼

發(fā)布時(shí)間:2021-08-31 09:26:56 來(lái)源:億速云 閱讀:114 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)如何編寫(xiě)PHP腳本清除WordPress頭部冗余代碼的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(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ù)解釋?zhuān)?/p>

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ù)的名稱(chēng)默認(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" />

這是隱性顯示的WordPress版本信息,默認(rèn)添加??梢员缓诳屠茫籼囟ò姹镜腤ordPress漏洞。清除代碼:

remove_action( 'wp_head', 'wp_generator' );

移除離線(xiàn)編輯器開(kāi)放接口

WordPress自動(dòng)添加兩行離線(xiàn)編輯器的開(kāi)放接口

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://jb51.net/xmlrpc.php?rsd" /> 
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://jb51.net/wp-includes/wlwmanifest.xml" />

 
其 中RSD是一個(gè)廣義的接口,wlwmanifest是針對(duì)微軟Live Writer編輯器的。如果你不需要離線(xiàn)編輯,可移除之。即便你需要使用離線(xiàn)編輯器,大部分時(shí)候也不需要這兩行代碼。Live Writer自己知道它們。保留這兩行代碼可能會(huì)留有安全隱患。清除代碼:

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)論的話(huà),則每條評(píng)論的鏈接都類(lèi)似于上述格式,只是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)簽,能有效的避免這類(lèi)問(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" title="feed名" href="http://jb51.net/feed/" />

來(lái)指定博客feed??梢员粸g覽器檢測(cè)到,然后被讀者訂閱。
如果你不想添加feed,或者想使用燒制的feed(如FeedSky或者Feedburner燒制的feed),可以移除之。

remove_action( 'wp_head', 'feed_links', 2 );//文章和評(píng)論feed 
remove_action( 'wp_head', 'feed_links_extra', 3 ); //分類(lèi)等f(wàn)eed

如果用的燒制的feed,然后還可以再手動(dòng)添加feed地址。

感謝各位的閱讀!關(guān)于“如何編寫(xiě)PHP腳本清除WordPress頭部冗余代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI