I was facing a problem with W3 Validator that constantly show some Warnings : ” The type
attribute is unnecessary for JavaScript resources.” in my WordPress Website. Also i have already another one that is: “The type
attribute is unnecessary for style resources”. In total i have 30 repeated Warnings that invalidate my website. After make some research i have found that HTML 5 doesn't require anymore to declare type attribute for JavaScript tags and style. So practically if you have:
<script type="text/javascript">
you have to remove the type=”text/javascript”
<script>
The same thing have to be done with style tags:
<style type="text/css">
and replace with:
<style>
Now with an html this is easy to edit and to fix the Warnings but isn't the same with WordPress that files are called with wp_enqueue_script or wp_enqueue_style functions. In this case WordPress gives to 2 hooks to give to us to remove the type attribute from JavaScript and style tags loaded from WordPress native functions.
The two hooks are:
style_loader_tag – HTML link tag of an enqueued style
script_loader_tag – Html ling tag of the enqueued script
So i created an function called codeles_remove_type_attr that with a reg expression remove the type attribute from WordPress loaded JavaScript and Styles tags.
After that i have added two filters one for the style tag hook and the other one for the script hook.
The code below have to be written into functions.php file.
add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2); add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2); function codeless_remove_type_attr($tag, $handle) { return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag ); }
Now the problem is resolved but all the WordPress plugins and themes have to do that so we can't fall in the problem to add the function in all of them manually.
Write in the comments below if you have any question or idea.
P.S Keep in mind that Warnings are not Errors so you can explain that to your clients too. Anyway there are clients that wants the w3c validator test clean.
Regards!
Ludjon, who co-founded Codeless, possesses a deep passion for technology and the web. With over a decade of experience in constructing websites and developing widely-used WordPress themes, Ludjon has established himself as an accomplished expert in the field.
Comments
Unfortunately, it does not work on my website.
Thanks its looks its works but not completely, I mean, I think on plugins still have these warnings, how can I solve it?
wp-includes/class.wp-scripts.php remove all ( type=’text/css’ )
wp-includes/class.wp-styles.php remove all ( type=’text/javascript’ )
Worst advice ever. Never hack the core.
Yes, this is a simple trick otherwise you have to edit the original code.
Didn’t work for us. Wait. I think I misunderstood what this does. This script basically removes it from any handcoded measures but won’t remove it from plugins? Correct?
I’ve been surfing online more than 2 hours today, yet I never found any
interesting article like yours. It is pretty worth enough for
me. In my opinion, if all website owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.
First time visiting your website, I enjoy your blog!
Bookmarked!, I really like your website!
WOW just what I was searching for. Came here by searching for meta_keyword|
Will this work with a Child theme that is based on a Elegant DIVI theme?
Hello,
We haven’t tested yet but I think it should work on Divi too.
Regards!
Hello, if you are is still interested, I solved the problem by adding html5 support:
add_action(
‘after_setup_theme’,
function() {
add_theme_support( ‘html5’, [ ‘script’, ‘style’ ] );
}
);
It works, but not alwqays on the inline style/script type (generated from plugins)