Hacking WordPress: Dofollow Whitelist for Commentator Links

[ Image: Inverted Eye Detail ] Before repenting of my filthy “nofollow” addiction, I experimented briefly with a “dofollow whitelist” for commentator URL links. The idea behind the whitelist is to reward frequent commentators, feed subscribers, site patrons, and other guests by selectively removing the automatically generated nofollow attributes from their associated comment-author links. For nofollow enthusiasts, a dofollow whitelist is a great way to show appreciation for people who support your blogging efforts.

Now, before we go hacking away at WordPress, keep in mind that there are a few potential shortcomings to this method. First of all, manually maintaining such a list would eventually fail. It simply would require too much work. Perhaps as an automated WordPress plugin, a dofollow whitelist would be a reasonable solution. A dofollow whitelist plugin would also eliminate the need to hack the WordPress core, which the following hack definitely requires. Other issues involve duplicate author names and user verification. Nonetheless, even as an elementary WordPress hack, a dofollow whitelist for comment signature links may prove useful. Here are a few examples:

  • Blogs wishing to encourage user registration may sweeten the deal by removing nofollow attributes from member links.
  • Sites featuring multiple guest bloggers may wish to serve dofollow links to their collective authorship.
  • With a simple modification, we can turn our “dofollow whitelist” into a “nofollow blacklist.”
  • Blog owners may wish to reward their favorite commentators with a little link love.
  • A savvy programmer with some free time may use it to develop a WordPress plugin. ;)

Thus, with its potential usefulness in mind, let’s hack ourselves a WordPress dofollow whitelist for comment signature links..

Hacking a WordPress dofollow Whitelist

Selectively serving dofollow links involves selectively removing WordPress’ automatically generated nofollow links. WordPress adds the nofollow attribute via the function get_comment_author_link(), which is 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. After locating the function, we find something similar to this:

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);
}

In this function, WordPress differentiates between linked comment signatures and unlinked (empty) comment signatures, formatting output accordingly. As you can see, when the comment author provides a URL, WordPress fashions a linked signature featuring the infamous external nofollow attribute. We need WordPress to further differentiate comment links based on whether or not the author is found on our whitelist. Sure enough, injecting an elseif() conditional statement does the trick:

// [ Dofollow Whitelist ] WordPress 2.0, 2.1, 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;
	elseif ( $author   == 'select username 1' 
			|| $author == 'select username 2' 
			|| $author == 'select username 3' 
			|| $author == 'select username 4' 
			|| $author == 'select username 5' 
		)
		$return = "<a href='$url' rel='external'>$author</a>";
	else
		$return = "<a href='$url' rel='external nofollow'>$author</a>";
	return apply_filters('get_comment_author_link', $return);
}

The new condition checks all comment authors against our username whitelist and serves a fully functional, dofollow link for each of them. Simply add and edit a new || $author == 'username' line for each selected name. As mentioned, I used this method for several weeks here at Perishable Press before finally deciding to eliminate nofollow attributes once and for all.