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!
Hey thanks man, you save me.
Keep up the good work
Pingback: Eliminar rel=”category tag” | Wp Es - WordPress en español
Very weird… I’m fairly confident that when I copy-pasted the above code from your blog to the terminal some invisible characters got their way into the code which confused the PHP parser.
After typing the lines of the function manually everything worked perfectly and my HTML5 validation errors got resolved. Thanks a ton for your solution, you saved me so much time!
This should totally work but for some reason I get:
2012/12/15 01:54:03 [error] 16852#0: *196955 FastCGI sent in stderr: “PHP Parse error: syntax error, unexpected ‘$output’ (T_VARIABLE) in /var/www/monda.hu/blog/wp-content/themes/beeblebrox/functions.php on line 12″ while reading response header from upstream, client: 84.3.8.128, server: monda.hu, request: “GET /blog/ HTTP/1.1″, upstream: “fastcgi://unix:/var/run/php-fastcgi/php-fastcgi.socket:”, host: “monda.hu”
which produces HTTP 500.
Thanks! saved my time