WordPress内容外链加上nofollow标签

考虑到SEO的细节,我们对于WordPress网站中的外部链接还是需要加上nofollow标签的,如果手工加太麻烦,这里可以用代码批量加上。

    //批量给外部链接加上nofollow
    add_filter('the_content','the_content_nofollow',200);
    function the_content_nofollow($content){
     preg_match_all('/href="(.*?)" rel="nofollow noopener" /',$content,$matches);
     if($matches){
      foreach($matches[1] as $val){
       if( strpos($val,home_url())===false ) //home_url为程序的url地址
       $content=str_replace('href="'.$val.'" rel="nofollow noopener"  rel="nofollow noopener" ', 'href="'.$val.'" rel="nofollow noopener"  rel="nofollow noopener"  rel="nofollow noopener" ', $content);
      }
     }
     return $content;
    }

投票 post
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享