Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

Industrial Strength WordPress Dofollow Upgrade

Encourage Comments by Completely Eliminating All Nofollow Links! Want to remove all traces of the hideous nofollow attribute without having to install yet another unnecessary plugin? By default, WordPress generates nofollow links in three different ways — this article will show you how to eliminate all of them..

Some context please..

If you are already familiar with the various functions involved in the nofollow-removal process, please feel free to skip the proceeding discussion and jump directly to the tutorial.

WordPress adds nofollow to all trackbacks, pingbacks, and commentator links

We have seen how simple it is to eradicate nofollow from comment-related content, which includes the three different types of $author URLs: trackbacks, pingbacks, and commentator links. In fact, WordPress generates hyperlinks for each of these comment-author URLs via the function get_comment_author_link(), which is conveniently located in the file wp-includes/comment-functions.php in WordPress 2.0 and wp-includes/comment-template.php in WordPress 2.1 and 2.2:

function get_comment_author_link() {
	global $comment;
	$url    = get_comment_author_url();
	$author = get_comment_author();

	if ( empty( $url ) || 'http://' == $url )
		$return = $author;
	else
		$return = "<a href='$url' rel='external nofollow'>$author</a>";
	return apply_filters('get_comment_author_link', $return);
}

Here, WordPress simply differentiates between linked and unlinked author data. Commentators with an associated URL receive a nasty nofollow link, regardless of link type (trackback, pingback, or commentator link). Those without an associated URL receive no link, but rather a text-only rendering of their name. Thus, our first type of link for which WordPress generates nofollow markup involves comment-related author links.

WordPress adds nofollow attributes to all unformatted, text-only comment links

The second type of link for which WordPress generates nofollow markup involves hyperlinks generated automatically from unformatted, text-only (non-hypertext) URL information. WordPress filters all comment content (post content is not filtered) for strings of text matching the generalized pattern of a URL: http://domain.tld/directory/file.html. Each detected instance of such a string is transformed into an actual hypertext link via the function make_clickable(), which is located in wp-includes/functions-formatting.php in WordPress 2.0, and wp-includes/formatting.php in WordPress 2.1 and 2.2:

function make_clickable($ret) {
	$ret = ' ' . $ret;
	// in testing, using arrays here was found to be faster
	$ret = preg_replace(
		array(
			'#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
			'#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
			'#([\s>])([a-z0-9\-_.]+)@([^,< \n\r]+)#i'),
		array(
			'$1<a href="$2" rel="nofollow">$2</a>',
			'$1<a href="http://$2" rel="nofollow">$2</a>',
			'$1<a href="mailto:$2@$3">$2@$3</a>'),$ret);
	// this one is not in an array because we need it to run last, for cleanup of accidental links within links
	$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
	$ret = trim($ret);
	return $ret;
}

Although it looks hideously complicated, the make_clickable() function simply checks comments for unformatted (non-hypertext/text-only) URLs and transforms them into genuine, clickable hyperlinks for all to enjoy. Of course, in the process of generating the links, WordPress takes the liberty of injecting those nasty nofollow tags. Yuck. It is also important to note that WordPress calls this function into action via the following filter (located in wp-includes/default-filters.php in WP 2+):

add_filter('comment_text', 'make_clickable');

WordPress adds nofollow attributes to all other comment-related links

The third and final type of link for which WordPress generates nofollow markup involves all hyperlinks within the comments themselves. Of course, hyperlinks are URLs that are originally expressed via anchor tags: <a href=""></a>. Every such link left within a WordPress-powered comment is generated by the function wp_rel_nofollow(), which is located in the file wp-includes/functions-formatting.php in WordPress 2.0:

function wp_rel_nofollow( $text ) {
	$text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
	return $text;
}

..and in wp-includes/formatting.php in WordPress 2.1 and 2.2:

function wp_rel_nofollow( $text ) {
	global $wpdb;
	// This is a pre save filter, so text is already escaped.
	$text = stripslashes($text);
	$text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
	$text = $wpdb->escape($text);
	return $text;
}

As we may see, wp_rel_nofollow() has been upgraded slightly in WP 2.1/2.2, but its overall functionality remains the same: add nofollow attributes to all hyperlinks located within comments. Further, WordPress calls upon this function via a filter located in the wp-includes/default-filters.php file (WP v2+):

add_filter('pre_comment_content', 'wp_rel_nofollow', 15);

Without that filter, WordPress cannot apply nofollow to links within comment content (hint, hint).

Getting on with it..

Okay, before we jump into the industrial-strength dofollow tutorial, let’s summarize the three different types of nofollow links and their associated functions:

  1. get_comment_author_link() → all trackbacks, pingbacks, and commentator links
  2. make_clickable() (applied via filter) → all unformatted (text-only) URL addresses
  3. wp_rel_nofollow() (applied via filter) → all other comment-related hyperlinks

And with that, we have a clear understanding of the functions involved in the nofollow-attribution process, as well as a nice plan of attack for our triune dofollow hack. Ready, let’s get on with it..

Tutorial: Complete nofollow removal

Before we begin, remember to backup your data! That way you are prepared in case anything weird happens. Always better to be prepared, as they say.

Step 1: Remove nofollow from all trackbacks, pingbacks, and commentator links

As per the first part of the preceding discussion, locate the function get_comment_author_link() within wp-includes/comment-functions.php (WP 2.0) or wp-includes/comment-template.php (WP 2.1/2.2). In that function, replace this line:

$return = "<a href='$url' rel='external nofollow'>$author</a>";

..with this one:

$return = "<a href='$url' rel='external'>$author</a>";

Step 2: Remove nofollow from all unformatted URLs

As per the second part of the preceding discussion, locate the function make_clickable() within wp-includes/functions-formatting.php (WP 2.0) or wp-includes/formatting.php (WP 2.1/2.2). In that function, replace these two lines:

'$1<a href="$2" rel="nofollow">$2</a>',
'$1<a href="http://$2" rel="nofollow">$2</a>',

..with these two lines:

'$1<a href="$2">$2</a>',
'$1<a href="http://$2">$2</a>',

Step 3: Remove nofollow from all other comment-related hyperlinks

As per the third part of the preceding discussion, locate the following filter within wp-includes/defualt-filters.php:

add_filter('pre_comment_content', 'wp_rel_nofollow', 15);

..and comment it out like so:

// add_filter('pre_comment_content', 'wp_rel_nofollow', 15);

That’s it, you’re done!

Upload your three files and check a few comment links to make sure everything went according to plan. If all of this seems like waay too much hassle to go through just to upgrade WordPress to dofollow status, you may prefer to simply take advantage of one of the many dofollow plugins that are currently available for WordPress users. Either way, removing nofollow tags is a great way to improve your blog. Cheers!

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Digging Into WordPress: Take your WordPress skills to the next level.

21 responses to “Industrial Strength WordPress Dofollow Upgrade”

  1. Custom Home Theater 2009/04/15 11:34 am

    I’m a little concerned that if I use this method, then what happens when there is a WordPress update.

  2. Jeff Starr 2009/04/19 9:38 am

    @Custom Home Theater: I suggest that you avoid hacking the core whenever possible. If you need to remove the nofollow attribute from WordPress, there are plenty of excellent dofollow plugins from which to choose.

  3. kelseymudd 2009/08/13 4:02 pm

    i think theme Switcher plugin is an excellent way to develop themes behind the scenes using WordPress. our first type of link for which WordPress generates nofollow markup involves comment-related author links.

  4. india tours 2009/09/04 4:34 am

    thanx for this great information.

  5. Jerry Boy 2009/09/13 8:40 pm

    I use the do-follow plugin on my main mmo blog, as a kind of experiment really. I want to see how it fares in the serps, even though it leaks a lot of juice.

  6. Jeff Starr 2009/09/14 8:38 am

    @Jerry Boy: Google recently announced a change in their nofollow policy. nofollow links no longer preserve any juice. Links are counted as links regardless of whether or not they are nofollow. The same amount of juice “leaks” for both nofollow and dofollow links. See this post for more information.

  7. Infographiste PAO 2010/02/06 8:22 am

    I didn’t know that Jeff, that’s a kind of great news, usually ppl don’t know about no or dofollow so now it’s gonna be alright without manipulating anything

  8. SEO newbie 2010/05/28 11:40 am

    So I have a question, i have done all of the above, yet i still have persistent nofollow tags throughout every link in my site.

    As i understand the new google nofollow status, link juice will still leak, but some will be dissipated, and some will leak, not nearly as much as if the attribute were not present. With this in mind, i think it would be very useful for my efforts if i were to find a way to remove all of these nofollow attributes.

    So, I guess my question to you is what files should i search that could bypass all of the above and add nofollow to all of my links (except adsense) site wide (text links in posts, navigation links, category links, comments, Everything…)?

    I have worked through …wp-includes/formatting.php, wp-includes/default-filters.php

    ps. your sites are pretty great.

  9. Jeff Starr 2010/05/29 9:38 pm

    Keep in mind this article is focusing on comments, trackbacks and pingbacks. Other links are not affected. I thought I saw a plugin awhile back that supposedly removed nofollow from theme template files. If you can locate it, it may help you clean up everything but post content. And then for that I am pretty sure there is a custom function available. Combined, these techniques will deliver a blog that is nofollow free. Er, or in your case, a blog that is entirely nofollow. As for editing the core files – keep track of things or even better don’t do it. Also remember that your theme files contain links and template tags that generate links.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.