在当前WordPress模板主题函数文件functions.php中添加以下代码,即可轻松给WordPress文章外链地址添加nofollow。
nofollow是HTML页面中a标签的属性值。它的出现为网站管理员提供了一种方式,即告诉搜索引擎"不要追踪此网页上的链接"或"不要追踪此特定链接"。这个标签的意义是告诉搜索引擎这个链接不是经过作者信任的,所以这个链接不是一个信任票。(本段来自百度百科)
//WordPress给文章外链添加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;
}