Snippet: Nofollow for Tag Cloud in WordPress

Tag clouds are good, but in a previous post called WordPress & Google Analytics: Tracking Your Tag Cloud I admitted that they’re not very useful, and especially for search engines — which is the topic I’ll discuss today.

I’m not an SEO junkie or anything but I do have some knowledge of the basic rules, one of which is avoid duplicate content. Now fire up your blog homepage — you’ve a list of your latest posts. Open the tag archive of some popular tag — you’ve got that very same list there. So for sure we have to ask Google not to index the tag archives, but we also have to ask Google not to follow links to such archives on every page.

The first step is done using robots.txt and according to your permalink structure. Mine for instance is /tag/tag-name so in order to disallow Google and other search engines to index that, I add the following to my robots.txt file:

User-agent: *
Disallow: /tag/

Please note that this is not a guide to the best robots.txt for your WordPress blog, and in reality you have to disallow more than just the tag directory (archives, search results, etc).

The second step, which is mainly why I wrote this post, is a short snippet for your functions.php file, that attaches a rel attribute with a nofollow value to every link produced by the wp_tag_cloud built-in function. This looks very much like the code snippet I shared in my last post only with a different regular expression. The two can be combined. Here’s the snippet:

function my_wp_tag_cloud($return) {
	$return = preg_replace('/(<a )/', '1rel="nofollow" ', $return);
	return $return;
}
add_filter('wp_tag_cloud', 'my_wp_tag_cloud');

Not the best regular expression ever, but that should work on the major themes out there. It looks for the opening tag of the anchor and simply attaches rel=nofollow, as easy as that ;)

Now save that and go back to your blog, view the source (Ctrl+U) and verify that it didn’t break anything. Now all your links in your tag cloud are marked as nofollow, thus each page containing the tag cloud will give away much less link weight to your tag archive pages (according to Google’s PageRank algorithm), oh by the way Larry Page is now CEO ;)

Update: Also, as Nathan and Andrew mentioned in the comments, there’s a wp_rel_nofollow function available since WordPress 1.5.0, so you might as well use that directly:

add_filter('wp_tag_cloud', 'wp_rel_nofollow');

Works like a charm, but the manual technique I’ve shown earlier allows you to add other relations to links in the tag cloud, for instance archives, or attach tracking code to your links for Google Analytics (a snippet introduced in an earlier post).

Hope that helps you out in your SEO journey.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

6 comments