6
  1. In Wordpress can I make default jquery "async"?
  2. Also can I change default version (1.12.x) of Jquery to the new one (2.2.x)?

Below is the reason why I`m asking:

Page speed insights want defer or async jquery

  • Ive found plugin Async Javascript - https://wordpress.org/plugins/async-javascript/, now Im testing it. – James Jan 28 '17 at 9:47
  • Yes, this plugin is working, but not with all sites. PageSpeed test not speaking about blocking scripts. – James Jan 28 '17 at 14:30
5

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.