2012年5月3日星期四

wordpress无插件实现给链接添加nofollow属性

 
 

satan 通过 Google 阅读器发送给您的内容:

 
 

于 12-5-3 通过 averiany涂鸦馆 作者:averainy

以前都是用插件实现的,但是用插件太占用服务器资源,google了一下,发现别人都总结好了,直接贴出来

//给标签云里的链接加上 rel="nofollow"  add_filter('wp_tag_cloud', 'cis_nofollow_tag_cloud');  function cis_nofollow_tag_cloud($text) {  return str_replace('<a href=', '<a rel="nofollow" href=', $text);  }     //给 the_tags() 生成的链接 加上 rel="nofollow"  add_filter('the_tags', 'cis_nofollow_the_tag');  function cis_nofollow_the_tag($text) {  return str_replace('rel="tag"', 'rel="tag nofollow"', $text);  }     //给 wp_list_categories() 生成的链接加上 rel="nofollow"  add_filter( 'wp_list_categories', 'cis_nofollow_wp_list_categories' );  function cis_nofollow_wp_list_categories( $text ) {     $text = stripslashes($text);  $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);  return $text;  }     //给 the_category() 生成的链接加上 rel="nofollow"  add_filter( 'the_category', 'cis_nofollow_the_category' );  function cis_nofollow_the_category( $text ) {     $text = str_replace('rel="category tag"', "", $text);  $text = cis_nofollow_wp_list_categories($text);  return $text;  }     //给 the_author_post_link 生成的链接加上 rel="nofollow"  add_filter('the_author_posts_link', 'cis_nofollow_the_author_posts_link');  function cis_nofollow_the_author_posts_link ($link) {  return str_replace('</a><a href=', '<a rel="nofollow" href=', $link);  }     //给 comments_popup_link_attributes() 生成的链接加上 rel="nofollow"  add_filter('comments_popup_link_attributes', 'cis_nofollow_comments_popup_link_attributes');  function cis_nofollow_comments_popup_link_attributes () {  echo ' rel="nofollow"';  }  //给外部链接添加nofollow属性  add_filter('the_content','web589_the_content_nofollow',999);  function web589_the_content_nofollow($content){  	preg_match_all('/href="(.*?)"/',$content,$matches);  	if($matches){  		foreach($matches[1] as $val){  			if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);  		}  	}  	return $content;  }

来源1:http://www.iidudu.com/303.html

来源2:http://www.cnbloge.com/3959.html

 


本文作者:averainy | 本文地址: 固定链接 | 我的腾讯微博|我的google+
本站文章除特殊标明者外均为原创,版权所有,如需转载,请以超链接形式注明作者和原始出处及本声明

相关日志


 
 

可从此处完成的操作:

 
 

没有评论:

发表评论