wordpress博客文章中外链添加nofollow属性

只要在主题的functions.php文件中添加下列代码即可,代码只会对外链添加external 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;
}


你可能感兴趣的:(wordpress博客文章中外链添加nofollow属性)