批量自动给WordPress外链文章添加nofollow标签

从SEO角度考虑,如果是外链的内容我们需要加上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;
}

这样,我们所有的外链内容在文章中的就会自动加上nofollow。


【AD】丽萨主机英国VPS推荐 英国原生IP双ISP家宅主机

【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工