WordPress Plugin: Nofollow

// January 19th, 2005 // Techie

I’m not going to bother posting this as a downloadable plugin because it’s not worth it, but here’s a simple WordPress plugin you can use to add Google’s “nofollow” relative attribute to all links within comments:


< ?php
/*
Plugin Name: NoFollow
Plugin URI: http://www.gudlyf.com/archives/2005/01/19/wordpress-plugin-nofollow/
Description: Adds the "nofollow" relative attribute to links left in comments
(See http://www.google.com/googleblog/2005/01/preventing-comment-spam.html)
Version: 1.0
Author: Keith McDuffee
Author URI: http://www.gudlyf.com
*/

function nofollow($text) {

        $text = preg_replace('/<a /i','<a rel="nofollow" ',$text);
        return $text;
}

add_filter('comment_text', 'nofollow');
?>

I also recommend editing the comment_author_link() and comment_author_url_link() functions in the wp-includes/template-functions-comment.php file to read:


function comment_author_link() {
        global $comment;
        $url = apply_filters('comment_url', $comment->comment_author_url);
        $author = apply_filters('comment_author', $comment->comment_author);
        if (!$author) $author = 'Anonymous';

        if (empty($url)) :
                echo $author;
        else:
                echo "<a href='$url' rel='external' rel='nofollow'>$author</a>";
        endif;
}
function comment_author_url_link($linktext='', $before='', $after='') {
        global $comment;
        $url = apply_filters('comment_url', $comment->comment_author_url);

        if ((!empty($url)) && ($url != 'http://') && ($url != 'http://url')) {
        $display = ($linktext != '') ? $linktext : stripslashes($url);
                echo "$before<a href='$url' rel='external' rel='nofollow'>$display</a>$after";
        }
}

5 Responses to “WordPress Plugin: Nofollow”

  1. TDavid says:

    CAPTCHA and the anti-URL code, now that’s protection!

  2. Tom Raftery says:

    Keith, there is an error in the code of this plugin – the first line “< ?php” shouldn’t have a space between the “<” and the “?” so it should be “

    Hope this helps,

    Keep up the good work,

    Tom

  3. Tom Raftery says:

    I’ll try again!

    Keith, there is an error in the code of this plugin – the first line shouldn’t have a space between the “<” and the “?”. Anyone who copyies and pastes this code will have problems unless they realise the error is there and remove the space.

    Hope this helps,

    Keep up the good work,

    Tom

  4. Tom Raftery says:

    Last time,

    Everytime I try to post this comment, it gets truncated because I’m putting in snippets of the code from the plugin above!

    This time no code – there is an error in the code of this plugin – the first line shouldn’t have a space in it.

    Anyone who copyies and pastes this code will have problems unless they realise the error is there, and remove the space.

    Hope this helps,

    Keep up the good work Keith,

    Tom

  5. MichaelE says:

    Once again, thanks for the great plug in. Got Trackback wacked this morning and made some fixes for that but the use of this plugin will atleast prevent the dirty SOB’s from gaining their objective.