Industrial Strength WordPress Dofollow Upgrade

Post #402 categorized as Function, WordPress, last updated on Nov 5, 2007
Tagged with comments, dofollow, hack, links, nofollow, php, tutorials, WordPress

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..

Note: 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 attributes 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 (WordPress 2+):

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!

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 URL addresses

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!

Subscribe to Perishable Press


15 Responses

TopLeave a comment

[ Gravatar Icon ]

#1Tony Tsai

That’s a good idea, using do follow to encourage other webmaster to comment, however, this would probably only apply to webmaster related blogs where techies are actually looking for blogs with no nofollow

[ Gravatar Icon ]

#2Tony Tsai

Btw, I enjoy your blog a lot, but I find it really hard on my eyes because of the background and font color…. Think it is something that you can work it out for the readers :)

[ Gravatar Icon ]

#3Perishable

Yeah, it is a bit on the geeky side of things, but I was having fun tweaking code and thought I would share it with everyone. Hopefully someone will find it useful ;)

As for the current theme’s background and font color, I have provided a way for users to “turn the lights on” (or off) by clicking the small sun icon in the lower right-hand corner of the browser. It requires JavaScript, but is the best I can do until I find time to get my alternate themes back up.

[ Gravatar Icon ]

#4Home Theater Tech

Thanks for the tip - I like to encourage comments as long as they aren’t promotional of “the usual suspects” like sex & drugs — but I don’t mind Rock n Roll :)

BTW - thanks for the sun Icon - I too like reading your site but on a sunny day the daytime ambient light from the large picture window here in my living room makes the white on black a bit hard to read. Way cool way to implement the change too with the sun and moon icons. I’d seen it before but thought it was just a “touch of style” and didn’t check clicking on it. If you’ve posted its purpose elsewhere I must have overlooked it.

[ Gravatar Icon ]

#5Perishable

Thanks for positive feedback, Home Theater Tech ;) I am glad that you find the alternate style sheet useful. Although I haven’t formally posted anything about it, I have mentioned it elsewhere in the comments. Several readers had voiced concern about the low-contrast text/background, so I finally spent a few hours to implement the CSS-switcher as a solution. I tend to downplay aspects of the site related to design and whatnot, however, I think I may write a series of articles focused on a few of the more popular and/or useful aesthetic site features.

[ Gravatar Icon ]

#6Ecommerce templates

Removing nofollow may be one of the best thing one can do in terms of comments and it really gets to the bottom of the internet idea. Nofollow is artificial.

[ Gravatar Icon ]

#7Perishable

Indeed, Ecommerce templates, nobody benefits when unscrupulous people strive to exploit the generosity of people who actually “get” the mutually beneficial nature of the socially driven blogosphere. Unfortunately, removing the “artificial” nofollow attribute opens the door to a whole new breed of artificiality.

[ Gravatar Icon ]

#8Ecommerce templates

Yes, but here comes the human factor and spam filters and I think it is enough to make it work, no?

[ Gravatar Icon ]

#9Perishable

Yes, most definitely! ;)

[ Gravatar Icon ]

#10University Jobs

Hey,
I checked your new design. It is colourful but it is little dark . Sometimes it is hard to see the text. But it surely looks like a Pro’s design.

[ Gravatar Icon ]

#11Chicago salon equipment

WOW!
Your design is really, really cool!
I just like it so much, the graphics are A-W-E-S-O-M-E!

As of the nofollow: it’s definitely so artificial and I think that it should be removed from blogs. It was a “quick-fix” and maybe it brought more damage to the internet community then benefit.

And yes, new problems arise with use of dofollow links, but if you ask me, it’s worth the price :)

[ Gravatar Icon ]

#12Home Theater Tech

Just dropped back in due to “email notification of new posts” and I too REALLY like the new look…. rather cosmic. ;)

[ Gravatar Icon ]

#13Custom Home Theater

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

[ Gravatar Icon ]

#14Jeff Starr

@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.

Trackbacks / Pingbacks

  1. Make it Do-Follow - DIY Themes Forums

Share your thoughts..

TopRead official comment policy

The rules are simple. Comment intelligently. Stay on-topic. Don’t spam! Suspected spam will be deleted. Use your real name or nickname, not a site name or business name. Using a site name or business name is a good way to get your link or comment removed. Certain comments are moderated; if your comment does not appear after several days, or if you wish to comment privately, contact me. Also, by posting a comment, you grant this site a perpetual license to reproduce your comment, name, and website URL. Lastly, you may use basic HTML markup, but please do not use <pre> tags. Instead, wrap your code with <code> tags. Use a new set of <code> tags for each code term or phrase, as well as for each individual line of code (i.e., multiple lines of code require multiple code tags). Please see the complete comment policy for more information.