Like the blog? Get the book »

WordPress Tip: Remove nofollow Attributes from Post Content

WordPress Tip: Remove nofollow Attributes from Post Content

If you have posts that include the nofollow attribute on links, you may at some point decide to remove them. By default, WordPress doesn’t insert nofollow attributes in post content, but there are a variety of plugins that will insert nofollow into all links in post content.

Or perhaps you have been manually adding nofollow tags to your post links for SEO purposes. Regardless of how they got there, it’s very easy to clean things up and remove all nofollow attributes from post content.

Remove nofollow attributes from post content

To clean up your post content, simply add the following code to your active theme’s functions.php file:

function remove_nofollow($string) {
	$string = str_ireplace(' rel="nofollow"', '', $string);
	return $string;
}
add_filter('the_content', 'remove_nofollow');

That’s all there is to it — no further editing required. Once in place, this function will filter all post content and remove any instances of rel="nofollow". Note that if you have some posts about nofollow, this function will remove rel="nofollow" from your code examples as well.

Remove nofollow attributes from post content and comment text

It’s just as easy to remove nofollow tags from comment text by including another add_filter function:

function remove_nofollow($string) {
	$string = str_ireplace(' rel="nofollow"', '', $string);
	return $string;
}
add_filter('the_content', 'remove_nofollow');
add_filter('comment_text', 'remove_nofollow');

With the additional add_filter, this code will eliminate all instances of rel="nofollow" that would have otherwise appeared in your post content and comment text.

18 responses

  1. thank you for this nice script ;)

    i am just thinking loud:

    • does this filter work with file or opcode caching?
    • how does wp write nofollow in db? and why?
    • how can we supress writing nofollow relations into wp-db?

    what can happen, if we allowed comment urls with no follow? why should be this useful? i mean i understand the work of this filter, but why should pp turn off nofollow? thank you, a good ping to you. is this the only way, we can turn on or off nofollow via this filter?

  2. str_ireplace is PHP5 only. Its better to use str_replace.

  3. Some links may have a rel attribute with "external nofollow" or "nofollow external". I’ve written a regex to catch these cases.

    • One could also just add a couple more lines to this method to match those cases. Either way could be useful when dealing with external attribute values.

  4. Thanks for the quick tutorial, Jeff! I’m just wondering – I actually though of using preg_replace instead of str_replace to remove the nofollow rel attribute. Is there any difference (performance or otherwise) when it comes to using preg or str_replace?

    • Good question. I have read that str_replace() is optimal for string replacements (as in this case), and that preg_replace() is optimal for regex replacements.

      • Oh boy, thanks so much for the rule of thumb. Looks like I’ve got some code optimisation to do today :) I’ve been replacing strings using preg_replace *facepalm*

        Anyway, thanks again for the help!

  5. great tip, guys. thanks. i know many following your blog are sophisticated with WP, but not everyone knows what ‘no follow’ is and why it might be desireable to remove it. or not.

  6. I’m not sure why WordPress insists in maintaining the nofollow comment attribute as its default. This filtering you describe would also remove the nofollow attribute in comment replies as well wouldn’t it? I noticed in your source code that you have the nofollow attribute in your comment replies. Also interesting use of ‘external nofollow’ for your comments here ;)

    • Yes, as explained in the article, the second method will remove the nofollow attribute from comments. WordPress adds the external attribute as well.

  7. Hi,

    I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “PHP functions str_ireplace and str_replace”.

    str_ireplace and str_replace both functions take 3 parameters.

    • Parameter1 : The input string which you want to search
    • Parameter2 : The string which you want to over write on the search string
    • Parameter3 : The original String

    What is the difference between str_ireplace and str_replace:

    The str_replace function is a case-sensitive which means that it replaces the string that exactly matches the string exactly. It means that if you call the function to replace the string “MFS” with “abc”, the function would not replace strings which consist of “mFS”, “mfs”, “MfS” because it wouldn’t consider them a complete match. However, the str_ireplace function php is not sensitive-rule and will treat “MFS”, “mfs”, “MfS” ….. all combination as a single match.

    Hope you find it useful and of assistance.

    Thanks,
    Bijayani

  8. what is the nofollow? what it is use for? and why do i need to remove them?

    • They are attributes placed on the anchor tag in various parts of WordPress. They are used to communicate to search engines that the link is not necessarily “vouched for” by the author. WordPress includes them in comments by default. You don’t need to remove them unless you understand them and know what you are doing. There is no need to remove them if you do not want to.

  9. Good article but I would like to suggest you using one of my plugin NoFollow Free heavily customizable and very simple to use (just install and go)

    • It’s actually easier to just add the code, but your plugin is great and provides lots of additional benefits for the user. Recommended.

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace