WordPress外部链接自动添加nofollow标签

追格官方小助手/ 2022年07月01日/ WordPress/ 浏览 1392

WordPress外部链接自动添加nofollow标签,目的是为了减少权重流失,我们只需将下面代码,添加到当前WordPress主题 Functions.php 文件中即可。


//外部链接添加nofollow
add_filter('the_content','zhuige_the_content_nofollow',999);
function zhuige_the_content_nofollow($content){
  preg_match_all('/href="(.*?)" rel="external nofollow" /',$content,$matches);
  if($matches){
    foreach($matches[1] as $val){
      if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"nofollow\" ",$content);
    }
  }
  return $content;
}


发表评论

暂无评论,抢个沙发...

客服 工单