WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm updating my self-built theme to HTML5, and easily made a number of changes already.

My theme's sole stylesheet is added to the page head via wp_enqueue_style in the functions.php file, as follows:

function gridded_enqueue_styles() {
    wp_enqueue_style('style', get_template_directory_uri() . '/style.css', array(), null, 'all');
add_action('wp_enqueue_scripts', 'gridded_enqueue_styles');

The resulting markup is:

<link rel='stylesheet' id='style-css'  href='http://grantpalin.com.dev:81/wp-content/themes/gridded/style.css' type='text/css' media='all' />

The stylesheet is loaded, the page renders, all good. However, the HTML5 spec indicates that the type attribute is no longer needed for the link, style, and script elements.

Looking through the codex reference, I don't see a way to alter or remove the type attribute. Can this even be done?

The obvious workaround is to hard-code the stylesheet reference in my header.php, but that's avoiding the benefit of the enqueue functionality.

share|improve this question
up vote 3 down vote accepted

For modify this output you can use that filter:

function wpse51581_hide_type($src) {
    return str_replace("type='text/css'", '', $src);
add_filter('style_loader_tag', 'wpse51581_hide_type');

You can see this hook into wp-includes/class.wp-styles.php

share|improve this answer
Thank you. That worked nicely. On a parallel tack, do you know if there is any way to do the same for script tags? There appears to be no script_loader_tag hook available, but there is script_loader_src, which I also tried to use, but produced no effect. – Grant Palin May 10 '12 at 6:13
@GrantPalin I looked in the source code, script_loader_tag filter does not exist. Unfortunately, the entire string is hardcoded link – user15194 May 10 '12 at 11:59
Is there much of a performance hit by using str_replace or preg_replace to strip the type attributes out? – Andrew May 31 '12 at 10:55
Not very much, see this replacement methods comparison. However, I would suggest using str_replace for better readability and slightly better performance. But preg_replace could be prefered provided you want to remove both " and ' -quoted attributes, e.g. preg_replace('/\s+type=["\']text\/css["\']/', '', $src), and don't want to run str_replace twice. – Adam Feb 12 '13 at 19:27
add_filter('style_loader_tag', 'html5_style_tag');

function html5_style_tag($tag)
    return preg_replace('~\s+type=["\'][^"\']++["\']~', '', $tag);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.