How to fix the rel=”category tag” in WordPress to allow W3C validation

21
Jul

This code snippet will present you with a solution for the rel=”category tag” tag found in the WordPress core that does not pass the HTML5 beta validation service.

The Issue with the rel=”category tag” HTML syntax

Whilst trying to validate a theme I have been working on through the W3C HTML5 validation service I came across an issue where it was found that the core WordPress system does not validate using the beta HTML5 online validation service due to the way that WordPress constructs the rel=”” tag.

The HTML5 spec states that only certain rel types are allowed and WordPress’ “category tag” isn’t one of them.

The solution

What we need to do now is add a custom function to our current theme’s functions.php file.
Navigate to “wp-content -> themes -> your-current-theme” and open up your themes “functions.php” file.
Scroll to the end of the file and add the following code:

1
2
3
4
5
add_filter( 'the_category', 'replace_cat_tag' );
 
function replace_cat_tag ( $text ) {
$text = str_replace('rel="category tag"', "", $text); return $text;
}
add_filter( 'the_category', 'replace_cat_tag' );

function replace_cat_tag ( $text ) {
$text = str_replace('rel="category tag"', "", $text); return $text;
}

What this code does is strips out or removes the current rel=”category tag” as a filter in the functions.php file for the theme.
By adding it to the functions.php for the theme this provides a theme wide fix rather than having to edit individual templates.

Be sure to check out our other great WordPress Code Snippets and WordPress Articles.

Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

10 Responses to How to fix the rel=”category tag” in WordPress to allow W3C validation

  1. Are there any repercussions if your theme/website does not pass the HTML5 beta validation service?

    Reply
  2. When using the Responsive Theme v1.8.1 (http://themeid.com/responsive-theme/) with WordPress 3.4.2 and the W3C HTML5 Validation Service (http://validator.w3.org) as of 2012-10-28, I found that WordPress was producing rel values of just “category” rather than “category tag.” Changing the function to the following worked for me:

    add_filter( ‘the_category’, ‘replace_cat_tag’ );

    function replace_cat_tag ( $text ) {
    $text = str_replace(‘rel=”category”‘, “”, $text); return $text;
    }

    Reply
  3. Its much more better practice if you add this to wp-includes/category-template.php. Thus you dont have to add it to every Theme you have loaded.

    Thank you for the article…

    Reply
    • I am not sure if I agree with you on this one. It is never a good idea to make edits to the core WordPress code. They will be overwritten with the next update.
      Better to add it to your themes functions.php file.

      Reply
  4. you rock. Thanks for sharing.

    Reply
  5. Thanks for the code man, it worked for me :)

    Reply
  6. Thanks for the code. Now my theme is Passed, 1 warning(s). Hahahhahaha.

    Reply
  7. web tasarımı

    wonderfull code man thanks .

    Reply

Leave a Reply