我們在製作wordpress主題的時候,會在頭部區域添加 wp_head() 函數,這個函數中包含了許多wordpress內置的功能函數,同時也可以在其中添加一些自定義的功能。
但是,有些功能是我們用不到的,如果加載只會增加頁面代碼的複雜性,影響我們的調試,也不利於代碼整潔。
完整的wordpress頭部清理代碼:
remove_action( 'wp_head', 'wp_generator' ); //WordPress版本信息。
remove_action( 'wp_head', 'parent_post_rel_link' ); //最後文章的url
remove_action( 'wp_head', 'start_post_rel_link' ); //最前文章的url
remove_action( 'wp_head', 'adjacent_posts_rel_link' ); //上下
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head');//上下文章的urlremove_action( 'wp_head', 'feed_links_extra', 3 );//去除評論feed
remove_action( 'wp_head', 'feed_links' ); //去除文章的feed
remove_action( 'wp_head', 'rsd_link' ); //針對Blog的離線編輯器開放接口所使用
remove_action( 'wp_head', 'wlwmanifest_link' ); //如上
remove_action( 'wp_head', 'index_rel_link' ); //當前頁面的url
remove_action( 'wp_head', 'wp_shortlink_wp_head' ); //短地址
remove_action( 'wp_head', 'rel_canonical');
wp_deregister_script('l10n');
remove_filter( 'the_content', 'wptexturize'); //禁用半角符號自動轉換爲全角
remove_action( 'wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
把這段代碼插入到主題的functions.php文件下,就可以清除WordPress頭部很多的冗餘信息。下面說說這些代碼的具體意義是什麼,以免刪除某些你想保留的功能。
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的默認函數,並可能用其它函數取而代之。 重要:添加hook時的$function_to_remove 和$priority參數要能夠相匹配,這樣纔可以移除hook。該原則也適用於過濾器和動作。移除失敗時不進行警告提示。 文章來自http://www.life134.com 參數 文章來自http://www.life134.com
返回值 (布爾值)函數是否被移除。 文章來自http://www.life134.com
|
移除WordPress版本信息
在head區域,可以看到如下代碼:
<meta name="generator" content="WordPress 3.1.3" />
這是隱性顯示的WordPress版本信息,默認添加。可以被黑客利用,攻擊特定版本的WordPress漏洞。清除代碼:
remove_action( ‘wp_head’, ‘wp_generator’ );
移除前後文、第一篇文章、主頁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.
移除feed
HTML中通過<link rel="alternate" type="application/rss+xml" title="feed名" href="http://example.com/feed/" />來指定博客feed。可以被瀏覽器檢測到,然後被讀者訂閱。
如果你不想添加feed,可以移除之。
remove_action( ‘wp_head’, ‘feed_links’, 2 );//文章和評論feed
remove_action( ‘wp_head’, ‘feed_links_extra’, 3 ); //分類等feed
移除離線編輯器開放接口
WordPress自動添加兩行離線編輯器的開放接口
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp-includes/wlwmanifest.xml" />
其中RSD是一個廣義的接口,wlwmanifest是針對微軟Live Writer編輯器的。如果你不需要離線編輯,可移除之。即便你需要使用離線編輯器,大部分時候也不需要這兩行代碼。Live Writer自己知道它們。保留這兩行代碼可能會留有安全隱患。清除代碼:
remove_action( ‘wp_head’, ‘rsd_link’ );
remove_action( ‘wp_head’, ‘wlwmanifest_link’ );
移除Canonical標記
remove_action( ‘wp_head’, ‘rel_canonical’ );
通過這樣的修改後,wordpress主題頭文件就會輕快很多,也保證了主題的安全。