国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

編寫PHP腳本清除WordPress頭部冗余代碼的方法講解

2024-05-04 23:43:25
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了編寫PHP腳本清除WordPress頭部冗余代碼的方法講解,很多meta信息于用戶于SEO都無實際用處,放在那里還有礙觀瞻...需要的朋友可以參考下
 

wordpress頭部的代碼非常多,包括WordPress版本,前后文、第一篇文章、主頁meta信息等各種冗余代碼,這些對博主來說是沒有意義的,也對網站的安全有一定的影響,也一度不知道這些代碼是有什么作用、怎么來的和怎么刪除。

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')); } ?>

各函數解釋:

wp_head函數

wp_head() 是wordpress的一個非常重要的函數,基本上所有的主題在header.php這個文件里都會使用到這個函數,而且很多插件為了在header上加 點東西也會用到wp_head(),比如SEO的相關插件。但是,在wp_head()出現的這個位置,會增加很多并不常用的代碼??梢酝ㄟ^ remove_action移除這些代碼。

remove_action函數

函數原型:

remove_action( $tag, $function_to_add, $priority, $accepted_args );

該函數移除一個附屬于指定動作hook的函數。該方法可用來移除附屬于特定動作hook的默認函數,并可能用其它函數取而代之。參見remove_filter(), add_action() and add_filter()。
重要:添加hook時的$function_to_remove 和$priority參數要能夠相匹配,這樣才可以移除hook。該原則也適用于過濾器和動作。移除失敗時不進行警告提示。
參數

  • $tag(字符串)(必需)將要被刪除的函數所連接到的動作hook。默認值:None
  • $function_to_remove(回調)(必需) 將要被刪除函數的名稱默認值:None
  • $priority(整數)(可選)函數優先級(在函數最初連接時定義)默認值:10
  • $accepted_args(整數)(必需)函數所接受參數的數量。默認值:1

返回值

  • (布爾值)函數是否被移除。
  • Ttue 函數被成功移除
  • False函數未被移除

移除WordPress版本

在head區域,可以看到如下代碼:

<meta name="generator" content="WordPress 3.1.2" />

 

這是隱性顯示的WordPress版本信息,默認添加??梢员缓诳屠?,攻擊特定版本的WordPress漏洞。清除代碼:
remove_action( 'wp_head', 'wp_generator' );

移除離線編輯器開放接口

WordPress自動添加兩行離線編輯器的開放接口

<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' );

移除前后文、第一篇文章、主頁meta信息

WordPress把前后文、第一篇文章和主頁鏈接全放在meta中。我認為于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標記

09年2月份,Google,Yahoo及Microsoft三大搜索引擎聯合推出了一個旨在減少重復內容困擾的方法,這對于廣大站長來說不啻是個好事情,不用再擔心因為網站上有重復的內容而影響到網站頁面的權重了。
造 成重復內容的原因有很多,最常見的便是多個url地址指向了同一個頁面,比如:wordpress平臺下的一篇日志頁面,包括了文章及評論內容。每個評論 都可以有個固定的鏈接地址,,如果有多個評論的話,則每條評論的鏈接都類似于上述格式,只是commentID號有所不同,這些鏈接其實都是指向同一篇文 章的。蜘蛛來爬時,便會依次爬行一遍,這篇文章下如有10條評論,則爬了10次相同的頁面文章,相當于做了多次重復的工作,嚴重影響了抓取的效率,及耗費 了帶寬。
重復內容造成的結果必然是蜘蛛不愿意來爬,不同的url指向同一個頁面,也會影響到該頁面的權重。通過canonical標簽,能有效的避免這類問題。
需要注意兩點:

  • 允許指向不同的子域名,不允許指向其他域名
  • canonical屬性可以被傳遞

即A頁面聲明B為權威鏈接,B聲明C為權威網頁,那么C就是A和B共同的首選權威版本

如果你的WP版本在2.9之前,需要通過插件(上面已經提到)或者手工 Hack 主題的 header.php 文件來使得博客支持。

<link rel="canonical" href="<?php get_permalink()?>" />

在 WordPress 2.9 發布之后,WordPress 已經默認支持這一標簽了,我們無需做任何動作,主題就支持這一標簽。這對于文章固定鏈接的更改很有幫助,可以增加對搜索引擎的友好度。但是如果你覺得這個標簽對你無用,也可以移除之:

remove_action( 'wp_head', 'rel_canonical' );

移除feed

HTML 中通過

<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 );//文章和評論feed remove_action( 'wp_head', 'feed_links_extra', 3 ); //分類等feed

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



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 如东县| 富阳市| 贵州省| 桐柏县| 确山县| 逊克县| 揭西县| 丰台区| 天气| 芜湖市| 邵阳县| 南投市| 佛山市| 广南县| 长沙县| 太康县| 伊川县| 南部县| 通道| 嘉祥县| 永泰县| 大厂| 蕲春县| 潮安县| 信阳市| 边坝县| 土默特左旗| 门头沟区| 内丘县| 施秉县| 温宿县| 庆安县| 磴口县| 依兰县| 特克斯县| 赤水市| 当雄县| 嘉定区| 通州市| 通州市| 云安县|