Contents
If you are searching for this, I assume that you don’t need any further introduction on nofollow and dofllow links. The nofollow attribute is added to links to prevent spam. However, there are times that you want to remove it to encourage comments from your visitors. In this post, I’m going to show you how to remove nofollow attribute from links at the comment section of WordPress.
Here are the steps we are going to do to make the author’s name link become dofollow:
- Go to your theme’s function.php file
- Get to the bottom of that file
- Add the following code
add_filter('get_comment_author_link', 'binary_carpenter_remove_nofollow', 10, 1); function binary_carpenter_remove_nofollow($content) { $content = preg_replace("/(<a[^>]*[^\s])(\s*nofollow\s*)/i", "$1", $content); $content = preg_replace("/(<a[^>]*[^\s])(\s*rel=[\"\']\s*[\"\'])/i", "$1", $content); return $content; }
Now, if you save the file, you will see the nofollow attribute disappear in the link.
Let’s see the code in action:
As you can see that, after the code was inserted, the nofollow attribute was gone.
How to remove the nofollow attribute from links in the comments’ content
We can use the same strategy to remove the nofollow attribute from links that are located in the comments’ content. However, this time, you will need to add another filter. If you have added the function binary_carpenter_remove_nofollow in the functions.php file, there is no need to declare it again.
Here is the code to remove the nofollow from comments’ content’s links:
add_filter('comment_text', 'binary_carpenter_remove_nofollow');
And that’s all you need to do to remove the nofollow attribute.
Conclusion
As you can see, the nofollow attribute is a useful tool to fight spam (spammers tend to not leaving a link on your site if it’s nofollow). However, if you have good reasons to make the links dofllow, you have the method to do so without using a plugin.
I hope the post has been helpful. Please like and share if you find it valuable. Thanks!