如何自动将WordPress外部链接添加nofollow标签 不传递权重

基于SEO要求的考虑,我们最好是将外部的URL地址都要加上nofollow标签的,这样告诉搜索引擎是不会传递权重的,确保站内的权重不会外流。当然,我们可以在添加链接的时候手动添加nofollow标签,不过也有时候会忘记的。这里我们可以直接用代码实现功能。

//外部链接添加nofollow
add_filter('the_content','web589_the_content_nofollow',999);
function web589_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;
}

这里,我们直接用这个代码添加到 Functions.php 文件中,然后只要是外部的链接都会自动添加nofollow标签。

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