Hacking WordPress: The Ultimate Nofollow Blacklist

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

[ Image: Death-metal rocker drunk with power ] Several days ago, I posted an article explaining how to hack your own WordPress nofollow blacklist. Immediately thereafter, I published an elaborate article focusing on automatic methods of nofollow blacklisting via WordPress plugins. In this article, I expand on the original blacklist hack by incorporating functional differentiation between commentator links, trackbacks, and pingbacks. If anything, think of this as an exercise in hacking WordPress, rewarding in and of itself, if not otherwise entirely impractical. Of course, whenever possible, you should avoid hacking the WordPress core and install a plugin instead. ;) Nonetheless, it’s so much fun to hack that we simply could not resist posting just one more article involving nofollow attributes. But alas, we really should be moving along..

The Default Function..

First, dig up the default WordPress function get_comment_author_link(), which is found in wp-includes/comment-functions.php in WordPress 2.0 and wp-includes/comment-template.php in WordPress 2.1 and 2.2. In either case, the function is identical, giving us this tasty morsel:

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

If you have been keeping up with recent posts, this function should look very familiar: check author names for associated links and generate corresponding signature. Linked author names get a nice little nofollow tag, while unlinked names are displayed text-only. This function also generates the link for all trackback and pingback linkage. Now that we have found this gem, we are going to hack it into a lean, mean, link-differentiating machine!

The Ultimate Blacklist..

If you have been reading me for any length of time, you know that I could really stretch this out. I could, for example, discuss every aspect of the entire process, explaining each and every functional aspect in excruciating detail. Etc., Ad nauseum, and Et al. Fortunately, I will spare you the utter agony this time, because after it’s all said and done, it all comes down to a rather elementary copy-&-paste operation. I will explain how to add names to the various lists, but after that, you are on your own. I trust everyone will make it out alive ;) Of course, I love discussing this type of stuff, so feel free to hit me with the tuff stuff!

Alright, let’s get on with it. Here is the code with which we will be replacing the previously defined default function. Check it out:

// The Ulitmate Nofollow Blacklist
function get_comment_author_link() {
	global $comment;
	$url    = get_comment_author_url();
	$author = get_comment_author();

	if ( empty( $url ) || 'http://' == $url ) {
		$return = $author;
	} elseif ( $comment->comment_type == 'pingback' ) { 
		if ( $author == 'username 0' 
				|| $author == 'username 1' 
				|| $author == 'username 2' 
				|| $author == 'username 3' 
			) {
			$return = '<a href="'.$url.'" rel="external nofollow">'.$author.'</a>';
		} else {
			$return = '<a href="'.$url.'" rel="external">'.$author.'</a>';
		}
	} elseif ( $comment->comment_type == 'trackback' ) {
		if ( $author == 'username 0' 
				|| $author == 'username 1' 
				|| $author == 'username 2' 
				|| $author == 'username 3' 
			) {
			$return = '<a href="'.$url.'" rel="external nofollow">'.$author.'</a>';
		} else {
			$return = '<a href="'.$url.'" rel="external">'.$author.'</a>';
		}
	} else {
		if ( $author == 'username 0' 
				|| $author == 'username 1' 
				|| $author == 'username 2' 
				|| $author == 'username 3' 
			) {
			$return = '<a href="'.$url.'" rel="external nofollow">'.$author.'</a>';
		} else {
			$return = '<a href="'.$url.'" rel="external">'.$author.'</a>';
		}
	}
	return apply_filters('get_comment_author_link', $return);
}

After replacing (or commenting out) the default get_comment_author_link() function, you will enjoy granular control over each type of comment-author link. Sequentially, the blacklist version of the function processes commentator ($author) names that are not associated with any URL. Then, the function generates links for pingbacks, trackbacks, and commentator links, respectively. For each of these link categories, author names are checked against a blacklist. If the name is matched, a nofollow link is served; otherwise, a dofollow link is produced.

As is, this code removes all nofollow attributes in commentator links, unless of course the author name happens to match one of the generic blacklisted username examples (e.g., username 0, username 1, username 2, etc..), in which case a nofollow link will be served. Of course, customizing the various links types (trackbacks, pingbacks, and commentator links) is entirely up to you.

By differentiating the various link types via our “ultimate blacklist” function, we gain control over much more than nofollow attributes. For example, it is just as easy to add CSS hooks, JavaScript functions, and even additional markup. Of course, there are many possible uses for such flexibility, including the satisfaction of an insatiable urge to hack WordPress ;)

Finally, to use the blacklist functionality made possible with this code, simply replace/edit the currently provided instances of “username n” with those that are worthy of the dreaded nofollow attribute. Also, keep in mind that it is easy to transform the “ultimate nofollow blacklist” into the “ultimate dofollow whitelist” by swapping each instance of rel="external" with its corresponding rel="external nofollow".

Subscribe to Perishable Press


14 Responses

TopLeave a comment

[ Gravatar Icon ]

#1John

It seems this combined with counter based acceptance method would rule. Black list the bums, and then, say, after 5 posts (or user defined), the poster’s links are changed to follow…

[ Gravatar Icon ]

#2Perishable

Yes indeed, two plugins immediately come to mind: Lucia’s Linky Love and the much newer NoFollow Free, both of which are reviewed here and here. Either of these plugins provide functionality very similar to what you have described..

[ Gravatar Icon ]

#3Lina

This is great information - do you know if there is any such thing for blogger/blogspot blogs? I know, I need to switch over to Wordpress, but it is just so much work and I have been trying to avoid that at all costs ;) Thanks for the informative blog!

[ Gravatar Icon ]

#4Perishable

Lina,
I have yet to dive into the vast realms of Blogger/Blogspot blogs, so I really wouldn’t know if something similar to a nofollow blacklist exists for those platforms (sorry). You may want to dig around with Google or maybe even create something yourself.. Otherwise, I wouldn’t worry too much about switching to WordPress — word on the streets is that its time is gradually coming to an end..

[ Gravatar Icon ]

#5OMC² Design

Hi, thanks for the content really helpful, but I hesitated to read it when I see the frightening image maybe change it?

[ Gravatar Icon ]

#6Perishable

Hi OMC² Design, thanks for the feedback on the article and associated image. Tell me something, do you really think it’s that “frightening”? I actually found the image quite hilarious — perhaps even a bit absurd. I do apologize for any psychological trauma invoked by the image — perhaps it remains best served to an audience more appreciative of such subtle attempts at irony.

[ Gravatar Icon ]

#7James Morris

You can add nofollow to all blogroll links with this plugin I made http://jmoz.co.uk/blog/wordpress-blogroll-nofollow-plugin/8

[ Gravatar Icon ]

#8Perishable

Sweet! Thanks for sharing, James — will definitely check it out :)

[ Gravatar Icon ]

#9web hosting

First, the picture scared me a bit. Actually more than a bit, really freaked me out!

Secondly, are there many extensions for WP for the nofollow fix?

[ Gravatar Icon ]

#10Perishable

Well, that’s a blatant setup if I’ve ever seen one! Sure, there are many wonderful dofollow plugins for WordPress. In fact, I even wrote a comprehensive dofollow reference article breaking it all down for you. Enjoy!

[ Gravatar Icon ]

#11Flash design

I’m really confused on this area. I do think that the nofollow is not the right way to do it and it ruins it for a lot of other people. However many blogs I used to read got spammed so badly.
Super good looking blog by the way.

[ Gravatar Icon ]

#12DeViLnoAnGeL InFo

hi Jeff, earlier i sent you an email.
The email is asking you about how to changing ‘external’ relation in trackback/pingback to ‘external nofollow’ or ‘nofollow’ relation in trackback/pingback on wordpress 2.7.1

I hope you can teach me how to do this ASAP.

Best regards,

Zaldy Saindy Masali

[ Gravatar Icon ]

#13Jeff Starr

@DeViLnoAnGeL InFo: I am pretty sure that WordPress 2.7 applies the nofollow attribute to all commentator, pingback, and trackback links. Unless I am missing something here..?

[ Gravatar Icon ]

#14DeViLnoAnGeL InFo

well except for the trackback/pingback link don’t include ‘nofollow’ relation the trackback/pingback link contain ‘external’ relation. and i want to change the relation to ‘external nofollow’ or ‘nofollow’ relation. i’m sure it in comment-template.php or wp-trackback.php that i need to change. but not sure where it is place.

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.