Removing nofollow from WordPress comments

Last updated on December 15, 2009. Tags: , , , ,

Due to the proliferation of comment spams, WordPress made it imperative that all links in comments will automatically have the nofollow tag (rel="nofollow"). The logic is that, there are comment spams because the spammers are building backlinks for their or their clients' websites hoping that search engines will see these websites as relevant for having so many backlinks. By inserting the nofollow tag in the comment links, the comment spams will become useless.

This can be difficult for blog owners who decided to approve a comment from a guest who had something important, useful or wonderful idea posted in the comment. The blog owner would like to provide some link love to the website of that guest and yet, WordPress automatically imposes a nofollow tag. There's even no option in the Dashboard to switch on and off this nofollow tag.

With Akismet plugin getting more and more powerful in dealing with comment spams, plus the capability of blog owners to moderate comments, inserting nofollow tag can be too farfetched, like throwing all the fruits in the basket just because there are few rotten ones.

WordPress recommend a number of plugins to disable the nofollow tag but for some theme customizers and web developers such as myself, if the required customization is very small and there is a way to do something without a plugin, then why use plugin?

There are two parts of the comment where you can insert link. One is the author name. If you fill up the WordPress comment form with your name (required) and website (optional), when your comment is finally displayed, your name will be a hyperlink text pointing to your website. The other is the comment form itself. Here, you need to manually type HTML codes to insert links in the body of your comment.

Removing the nofollow tag from the comment author name

The comment author name always goes with the comment to indicate who wrote that comment. It's usually in the form of "Posted by Comment Author on ...". The underlined and bold text is a hyperlink pointing to the website of the comment author. If the comment author left the website form blank, then only the name will be displayed and it will not be a hyperlink. It is generated by the following code found in comments.php.

<?php comment_author_link() ?>

If a website URL was provided, WordPress will automatically insert the nofollow tag to the link generated by this code from the inputted comment author name and website. To disable the auto insertion of nofollow, replace this code with the one below:

<?php if ($comment->comment_author_url != "" && $comment->comment_author_url != "http://") : ?>
<a href="<?php comment_author_url(); ?>"><?php comment_author(); ?></a>
<?php else : ?>
<?php comment_author(); ?>
<?php endif; ?>

The comment_author_link() is within a single line of paragraph. It might be less difficult if you transform the code above into a single line of code before inserting it in place of comment_author_link() as shown below (though the multi line code will work just as fine).

<?php if ($comment->comment_author_url != "" && $comment->comment_author_url != "http://") : ?><a href="<?php comment_author_url(); ?>"><?php comment_author(); ?></a><?php else : ?><?php comment_author(); ?><?php endif; ?>

Most WordPress tempates (all that I've encountered so far) use comment_author_link() to insert the name of the author and the link to his/her website in the comment section of the post. If your template is using another line of code,  such as  get_comment_author_url() or comment_author_url_link(), then there is no need to edit your template because these codes do not insert nofollow (but they don't work in the same manner as comment_author_link()).

Removing the nofollow tag from the main comment body

The code that displays the actual comment posted by the blog visitor is:

<?php comment_text() ?>

Similar to the comment_author_link(), it also automatically insert nofollow in the link; it inserts nofollow tag in all links within the comment regardless if it is an internal link to one of your posts or an external link to another site.

To disable the automatic insertion of nofollow tag, open the functions.php of your theme and insert the following codes (as usual, either immediately below the first <?php or the very last ?>):

function commentdofollow($text) { $return = str_replace('" rel="nofollow">', '">', $text); return $return; }
add_filter('comment_text', 'commentdofollow');

After inserting and saving the functions.php, check if you have any comment with links within the body. Check the source code and see if the rel=nofollow is still there. If you did this correct, it should no longer be there. All links, internal or external will no longer have the nofollow tag.

Note that you might find comment_text_rss() instead of comment_text(). This works in the same way as comment_text() but it does not allow HTML coding. Meaning, it's not possible to insert links within the body of the content in templates that use comment_text_rss().

Important notes:

The WordPress template tags <?php comment_author_link() ?> and <?php comment_text() ?> automatically inserts nofollow tag. Note that this post was written at around the time of WordPress 2.8. It's possible that in some later versions of WordPress, there will be no longer auto insertion of nofollow tag, or at least there will be a way to turn on and off the nofollow tag without resorting to hacking the WordPress theme.

Posted by Greten on December 4, 2009 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.