DEV Community

Adam Mateusz Brożyński
Adam Mateusz Brożyński

Posted on

1

WordPress: remove type attribute for JavaScript resources

Problem:

  • W3 Validator says:
Warning: The type attribute is unnecessary for JavaScript resources.
Enter fullscreen mode Exit fullscreen mode

Solution:

  • In functions.php insert following code:
<?php 
function removeTypeJS($x) {
  ob_start();
  call_user_func($x);
  echo preg_replace("/type=['\"]text\/(javascript|css)['\"]/", '',ob_get_clean());
}
?> 
Enter fullscreen mode Exit fullscreen mode
  • Replace <?php wp_head(); ?> in template with: <?php removeTypeJS("wp_head"); ?>
  • Replace <?php wp_footer(); ?> in template with: <?php removeTypeJS("wp_footer"); ?>

Top comments (0)

Visualizing Promises and Async/Await 🤓

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await 🤓

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay