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
-
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?
-
str_ireplaceis PHP5 only. Its better to usestr_replace. -
Some links may have a
relattribute with"external nofollow"or"nofollow external". I’ve written a regex to catch these cases. -
Thanks for the quick tutorial, Jeff! I’m just wondering – I actually though of using
preg_replaceinstead ofstr_replaceto remove the nofollow rel attribute. Is there any difference (performance or otherwise) when it comes to using preg orstr_replace? -
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.
-
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 ;)
-
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_ireplaceandstr_replaceboth 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_ireplaceandstr_replace:The
str_replacefunction 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, thestr_ireplacefunction 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 -
what is the nofollow? what it is use for? and why do i need to remove them?
-
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)






