[wordpress技巧]优化wp_head 中无关紧要的代码

我们在制作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

  • 1.$tag(字符串)(必需)将要被删除的函数所连接到的动作hook。默认值:None
  • 2.$function_to_remove(回调)(必需) 将要被删除函数的名称默认值:None
  • 3.$priority(整数)(可选)函数优先级(在函数最初连接时定义)默认值:10
  • 4.$accepted_args(整数)(必需)函数所接受参数的数量。默认值:1

返回值

(布尔值)函数是否被移除。 文章来自http://www.life134.com

  • 1.Ttue 函数被成功移除
  • 2.False函数未被移除

 

移除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标记

在 WordPress 2.9 发布之后,WordPress 已经默认支持这一标签了,我们无需做任何动作,主题就支持这一标签。这对于文章固定链接的更改很有帮助,可以增加对搜索引擎的友好度。但是如果你觉得这个标签对你无用,也可以移除之:
remove_action( ‘wp_head’, ‘rel_canonical’ ); 


通过这样的修改后,wordpress主题头文件就会轻快很多,也保证了主题的安全。


发布了28 篇原创文章 · 获赞 8 · 访问量 10万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章