WordPress添加nofollow标签与外链跳转页面(代码版)

小初seo 网站建设评论1,123字数 857阅读2分51秒阅读模式

为了防止网站权重的流失,我们可以为外链添加 nofollow 标签,高速搜索引擎不要追踪此链接,除此之外,我们也可以加入外链跳转页面实现外链转内链并修改 robots 文件,更彻底的放债网站权重的流失。本文就分享一个不使用插件来实现这个目的的方法。给大家讲讲WordPress如何添加nofollow标签与外链跳转页面

自动为 WordPress 的所有外链都添加 nofollow 标签和 go 外链跳转,只需要在你的 functions.php 文件里加入以下代码:


//https://www.pkak.cn/  加nonofollow
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content) {
preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
if($matches){
foreach($matches[2] as $val){
if(strpos($val,"#")===false) {
$content = str_replace( "href=\"".$val."\"", " target=\"_blank\" "."href=\"".$val."\"", $content );
}
if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){
$content=str_replace("href=\"$val\"", " rel=\"nofollow\" href=\"$val\" ",$content);
}
}
}
return $content;
}$content;
}

这个代码会忽略内链,将外链自动添加 nofollow 标签和 go 跳转,并将所有链接都通过新标签页的方式打开。

 

 

 最后更新:2021-6-5
  • 本文由 小初seo 发表于 2021年4月20日19:05:53
  • 转载请务必保留本文链接:https://www.pkak.cn/web/93.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证