Puddinq.com sharing knowledge

Warning: The type attribute is unnecessary for JavaScript resources.

Warning: The type attribute is unnecessary for JavaScript resources.

By default WordPress adds scripts by using the type attribute enqueing styles and scripts, the latest html validators throw a warning because this is redundent/ unnecessary

Exmaple

Here is an example of the default way WordPress includes scripts

 

<!-- example: overcomplete -->

<script type='text/javascript' src="scriptsource"></script>

 

While this would be enough

 

<!-- example: enough -->

<script src="scriptsource"></script>

 

Solution

 
The following script (add to your functions.php) removes the unnecessary attribute
 

// remove type attribute enqueue
add_filter('style_loader_tag', 'puddinq_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'puddinq_remove_type_attr', 10, 2);

function puddinq_remove_type_attr($tag, $handle) {
    return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}