Archived topic
The type attribute is unnecessary for JavaScript resources
5 replies · Started by Ciomîrtan Andrei on February 13, 2019
Hi Tom,
I use validator for HTML
https://validator.w3.org/nu/?doc=https%3A%2F%2Fwww.refu.ro%2F
And the validator give me a warning:
The type attribute is unnecessary for JavaScript resources.
Where <script type="text/javascript"> is the problem for the not fully validate the blog.
This code in child functions.php help?
add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts' );
add_filter('style_loader_tag', 'myplugin_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'myplugin_remove_type_attr', 10, 2);
function myplugin_remove_type_attr($tag, $handle) {
return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}
Thanks!
Hi there,
these are only warnings, so won't stop your site from being validated. If you must remove them, then i found this snippet:
add_action( 'template_redirect', function(){
ob_start( function( $buffer ){
$buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'" ), '', $buffer );
return $buffer;
});
});
WordPress also natively supports HTML5 markup.
function theme_name_setup() {
add_theme_support( 'html5', array( 'script', 'style' ) );
}
add_action( 'after_setup_theme', 'theme_name_setup' );
Hi,
I believe Tom addressed this already in 3.0.
https://generatepress.com/forums/topic/the-type-attribute/page/2/#post-1483008
It is mentioned in the changelog.
"Tweak: Remove type attribute from scripts and styles"
https://generatepress.com/generatepress-3-0-a-new-era/
GP 2.2.2 and GPP 1.7.7 are pretty old. You may want to consider updating them.
Thanks Elvin. That's great.
No problem. :)