How to Remove NoFollow From Comment Author Links in WordPress

WP Engine
How to Remove NoFollow From Comment Author Links in WordPress

There are two types of links in the WordPress comments section. The first is the one pointing to the website of the comment author. It’s the optional URL you enter whenever you post a comment on the standard WordPress response form. The second is links within the actual comment itself.

Current “NoFollow” Policy

For several years, WordPress has followed a standard policy for both these kinds of links. It automatically “nofollows” them. This means that search engines are explicitly instructed not to follow through with these links and index them. The reason for this is that WordPress sites are easily targeted by spammers and hackers looking to obtain valid backlinks. The “NoFollow” policy of the WordPress comment section was meant to discourage this practice.

However as any owner of a WordPress site will tell you, this doesn’t seem to have any appreciable impact on the number of spam comments received. Rather, users have to use plugins like Akismet and other spam control techniques to stave off undesirable comments. So the continued use of “NoFollow” is questionable at best.

If you want to encourage comments on your blog, it doesn’t hurt to give commenters a bit of an additional perk. By removing the “nofollow” directive embedded in all WordPress comment author links, you show your commenters that participation in your site is a great idea. Over time, you can build up a strong community. This kind of tactic is especially useful for new blogs looking to build a reputation.

There are many WordPress plugins that allow you to remove the “NoFollow” attribute from WordPress comment author URLs. However, why clutter up your site with yet another plugin when a tiny bit of code can accomplish the same thing? Here’s how to go about it.

Removing “NoFollow” via Code

Normally, WordPress comments are wrapped in a rel=”nofollow” attribute attached to the HTML of a comment author link. For example, here’s what it looks like on my blog:

normally links are nofollow

In the screenshot above, we have an “external” attribute along with “nofollow”. In fact, it’s impossible to tell in advance whether the “rel” is going to have a single attribute, or more. Because of this complication, it’s not as easy as a simple search and replace. We have to take into account the fact that there can be multiple attributes for the “rel” tag. And maybe not a rel attribute at all!

Open your functions.php file and paste in the following code before the closing ?> PHP tag:

function remove_nofollow($return, $author, $comment_ID) {
        $return= preg_replace("/(<a[^>]*[^\s])(\s*nofollow\s*)/i", "$1", $return);
        $return= preg_replace("/(<a[^>]*[^\s])(\s*rel=[\"\']\s*[\"\'])/i", "$1", $return);
        return $return;
}
add_filter( 'get_comment_author_link', 'remove_nofollow');

If you don’t know how to add code like this in WordPress, check out my earlier tutorial on how to do so. This snippet simply hooks into the “get_comment_author_link” filter and uses some complex regular expression magic to remove the “nofollow” attribute from the “rel” tag. And if it happens to be the only attribute and we’re left with an empty “rel” tag, it removes that as well. After saving my functions.php with the new code added in, here’s what the same screenshot above looks like now.

nofollow removed

As you can see, the “nofollow” attribute has vanished but the “external” remains intact. If you’re wondering what the results would be like for a different string, you can check out this excellent preg_replace online tester. Simple use the three parameters of preg_replace to place in the fields of the site and see what the output is after running it through the two function calls!

This simple code changes all the comment author links on WordPress to “DoFollow”. It’s a great little perk for regular comments on new blogs, provided you take care to monitor spam.

Leave a Reply

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.