jawittdesigns
8/3/2013 - 2:13 PM

Nofollow for external links in comment text

Nofollow for external links in comment text

// source:http://www.onextrapixel.com/2012/10/12/5-code-snippets-for-interacting-with-wordpress-post-content-effectively/
function auto_nofollow($content) {
    //return stripslashes(wp_rel_nofollow($content));
    return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}
add_filter('comment_text', 'auto_nofollow');
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;
}