Defer Parsing of JavaScript in WordPress – new filter & old hack

When the defer attribute is present in the script tag, it specifies that the script is executed when the page has finished parsing. A requirement that is enforced by Google for example (Page-Speed ranking).

Currently the WordPress wp_enqueue_script provides no easy way to add new attributes, but there is a way around that :)
The below hack / filter needs to be added to your theme function.php. The filter should be your preferred solution, as the clean_url filter has been deprecated.

Make sure that your website / theme is still loading after adding these changes. If needed you can add more exceptions, as I did for jQuery.

Hack for WordPress before 4.1

  1. if(is_admin() === FALSE){
  2. function defer_parsing_of_js ( $url ) {
  3. if ( FALSE === strpos( $url, '.js' ) ) return $url;
  4. if ( strpos( $url, 'jquery.js' )  ) return $url;
  5. return "$url";
  6. }
  7. add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
  8. }

 Filter for WordPress 4.1+ 
Updated: 26.04.2015 – using clean Regexp

  1. if(is_admin() === FALSE){
  2. function add_js_defer($tag, $handle, $src){
  3.  if(  strpos( $tag, 'jquery.js' ) ) return $tag;
  4.   preg_match_all('/([\w\-.:]+)\s*=\s*("[^"]*"|\'[^\']*\'|[\w\-.:]+)/i', $tag, $match);
  5.                
  6.         $newtag = "<script ";
  7.        
  8.         foreach($match[1] as $key=> $val){
  9.                 $newtag .= $val."=".$match[2][$key]." ";       
  10.         }
  11.        
  12.         $newtag .= "defer></script>";
  13.        
  14.     return $newtag.PHP_EOL;
  15.  }
  16.  add_filter( 'script_loader_tag', 'add_js_defer', 11, 1 );
  17. }

Browser Support

  1. All modern browsers
  2. Support in IE9 is flaky, as the execution order isn’t guaranteed.
  3. Can I use
Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

3 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago