WordPress文章自动添加上nofollow的方法

文章中把链接自动添加上nofollow,这样对于SEO有一定的好处。

具体方法

add_filter('the_content', 'auto_nofollow'); //nofollow文章内容的站外链接
 
add_filter('comment_text', 'auto_nofollow'); //nofollow评论内容的站外链接
 
function auto_nofollow($content) {
    //return stripslashes(wp_rel_nofollow($content));
 
    return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}
 
function auto_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
 
    if (strpos($link, 'rel') === false) {
        $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
    } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

以上这段代码是文章和评论中的链接都会自动加上nofollow,可以根据需要进行调整。

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/1158.html

(1)
联系我们
联系我们
分享本页
返回顶部