A fun experiment to reduce W3C Validator warnings about the 'type' attribute of scripts and styles.
In the Facebook Advanced WooCommerce group a member asked about the large number of errors that the W3C Markup Validator was reporting. Many were warnings that the ‘type‘ attribute was unnecessary for <script> and <style> tags. I decided to try and fix this.
When I put the member’s site url into the validator I saw a lot of warnings:
Warning: The type attribute for the style element is not needed and should be omitted.
I wrote code to do a simple str_replace to remove these type attributes.
Going one better
I knew that the enqueued scripts and styles are output during the ‘wp_head‘ action so I would store all that output and then remove the attribute at the end. It starts capturing the output as early as possible and ends with a high action priority.
The two filters are still necessary as they will remove the ‘type‘ attribute from scripts and styles output in the footer.
Using a local installation of WordPress 4.9.8 with WooCommerce 3.5.1 and Storefront 2.3.5 I ran its markup through the W3C Markup Validator with and without my code.
Without my code it reports 28 warnings. With my code active this drops to 8 warnings.
Not all the warnings are for the ‘type‘ attribute. Furthermore, my code does not change inline code in the footer. I did a quick test to capture the ‘wp_footer‘ action output but it didn’t work. I am sure that it’s a small issue but this code is mostly a proof of concept.