Nofollow For Category Links No Plugin Required

For SEO reasons I really wanted to make this site’s category links nofollow. The easiest way is to find and use a plugin. That’s what normal – sane – people would do. But I like to tinker with WordPress because it helps me understand the system better and a whole lot deeper too. Well, I found several threads where Otto talked about adding the following code into the theme.

add_filter('wp_list_categories','wp_rel_nofollow');

Could it be that easy? I tried it but it didn’t quite work. It gave me a bunch of backslashes in the HTML. Thanks to the guidance of recordinghacks on this thread, the code below is now in my theme’s functions.php file. Works beautifully.

function tbm_rel_nofollow( $text ) {
    global $wpdb;
    // This is a pre save filter, so text is already escaped.
    $text = stripslashes($text);
    $text = preg_replace_callback('||i', 'wp_rel_nofollow_callback', $text);
    return $text;
}
add_filter('the_category','tbm_rel_nofollow');
Scroll to Top