删除WordPress《head》标签中的多余代码

WordPress头部的冗余代码非常之多,也一度不知道这些代码是有什么作用、怎么删除。现摘抄整理相关内容如下:

//移除WordPress版本,<meta name=”generator” content=”WordPress 3.3.2″ />

remove_action( 'wp_head', 'wp_generator' );

//移除head中的rel=”EditURI”,

//<link rel=”EditURI” type=”application/rsd+xml” title=”RSD” href=”http://example.com/xmlrpc.php?rsd” />

remove_action( 'wp_head', 'rsd_link' );

//移除WordPress自动添加的离线编辑器的开放接口

//<link rel=”wlwmanifest” type=”application/wlwmanifest+xml” href=”http://example.com/wp-includes/wlwmanifest.xml” />

remove_action( 'wp_head', 'wlwmanifest_link' );

//移除head中的rel=prev

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );

//移除rel=shortlink

remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );

造成重复内容的原因有很多,最常见的便是多个url地址指向了同一个页面,比如:wordpress平台下的一篇日志页面,包括了文章及评论内容。每个评论 都可以有个固定的链接地址,,如果有多个评论的话,则每条评论的链接都类似于上述格式,只是commentID号有所不同,这些链接其实都是指向同一篇文 章的。蜘蛛来爬时,便会依次爬行一遍,这篇文章下如有10条评论,则爬了10次相同的页面文章,相当于做了多次重复的工作,严重影响了抓取的效率,及耗费了带宽。

重复内容造成的结果必然是蜘蛛不愿意来爬,不同的url指向同一个页面,也会影响到该页面的权重。通过canonical标签,能有效的避免这类问题。

需要注意两点:
允许指向不同的子域名,不允许指向其他域名
canonical属性可以被传递
即A页面声明B为权威链接,B声明C为权威网页,那么C就是A和B共同的首选权威版本

这对于文章固定链接的更改很有帮助,可以增加对搜索引擎的友好度。但是如果你觉得这个标签对你无用,也可以移除之

//移除rel=canonical

remove_action('wp_head', 'rel_canonical' );

// 移除额外的feed,例如category, tag页

remove_action('wp_head', 'feed_links_extra', 3);

//文章和评论feed

remove_action( 'wp_head', 'feed_links', 2 );

以上代码直接放到主题的functions.php中。

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函数未被移除

全部详细代码收藏:

remove_action('wp_head', 'index_rel_link');//当前文章的索引 
remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页 
remove_action('wp_head', 'start_post_rel_link', 10, 0);// 开始篇 
remove_action('wp_head', 'parent_post_rel_link', 10, 0);// 父篇 
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇. 
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre 
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink 
remove_action('wp_head', 'rel_canonical' ); 
wp_deregister_script('l10n'); 
remove_action('wp_head','rsd_link');//移除head中的rel="EditURI" 
remove_action('wp_head','wlwmanifest_link');//移除head中的rel="wlwmanifest" 
remove_action('wp_head','rsd_link');//rsd_link移除XML-RPC 
remove_filter('the_content', 'wptexturize');//禁用半角符号自动转换为全角
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本

来自 http://devework.com/delete-head-wp_head-codes.html