Make your WordPress category list valid by removing the “rel” attribute

The Problem

WordPress is a great blogging platform, but like anything in life it does have its shortcomings and flaws. While trying to validate my site as HTML5, I ran into one such issue. The functions “the_category” and “wp_list_categories” use the file “/wp-includes/category-template.php” to output the HTML that lists all categories for a particular post or set of posts. If you take a look at that file (line 163, function “get_the_category_list”) , you’ll notice that it adds the attribute ‘rel=”category tag”‘ to the links that it produces. While “tag” is a valid value for rel, “category” is not according to this list found at microformats.org. This causes the W3 Validator to throw an error stating “Bad value category tag for attribute rel on element a: Keyword category is not registered.”

The Solution

How do we fix this problem? Simple – either remove the word “category” from the “rel” attribute, or remove the “rel” attribute all together. The following PHP code sample will do the latter, when placed in the current theme’s “functions.php” file. If you are not comfortable editing PHP code directly or messing with your theme, I plan to update this post sometime in the future with a plug-in that will do this automatically so please stay tuned.

Without further ado:

function remove_category_list_rel( $output ) {
	// Remove rel attribute from the category list
	return str_replace( ' rel="category tag"', '', $output );
}

add_filter( 'wp_list_categories', 'remove_category_list_rel' );
add_filter( 'the_category', 'remove_category_list_rel' );

This function works by using the PHP function “str_replace” to replace the “rel” attribute with an empty string and is called by registering a WordPress filter.

Hopefully you will find this little piece of code useful and my explanation clear enough to understand it. If you have anything to say, or you need help getting this to work, please add a comment using the form below and I will do my best to get back to you. Thanks for reading!