How can I make default jquery version of WordPress “async”?

So as far as I know, WP does not support “async” yet, for scripts. Which means the only way you’ll be able to add any script async is by including them directly in your theme’s header (or footer) file… And that is going to be a bit hard to manage, since a lot of queued scripts will require jquery – but since it won’t be queued by WP, then they just won’t print…

I would suggest going “sync” – most (all?) WP sites are like this and working fine. In any case, here’s how you’d do this…

First, unregister the default WP jquery:

function replace_jquery() {
  if(wp_script_is('jquery', 'registered')) wp_deregister_script('jquery');
  // I suggest forgetting about "Async" and qneuing your new jquery here:
  $new_jquery_location = '//code.jquery.com/jquery-2.2.4.min.js'; // could be a local file, whatever you like...
  $in_footer = true; // or false, your choice...
  wp_register_script('jquery', $new_jquery_location, array(), '', $in_footer);
  wp_enqueue_script('jquery');
}

add_action('wp_enqueue_scripts', 'replace_jquery');
add_action('admin_enqueue_scripts', 'replace_jquery');

Of course, another big warning here is to make sure everything works with the new jQuery version… You likely now will have themes or plugins (even WP itself) expecting an older version of jQuery, and they might not like the new version one bit…

Hope this helps!

Leave a Comment