19

I run WordPress version 4.7.2. and it uses jQuery version 1.12. I need to update this version to a higher one. I replaced it with a new version before, but when I upgrade WordPress core it is replaced with 1.12 again. How can I change the version of jQuery that WordPress uses permanently?

27

Warning: You shouldn't replace core jQuery version, especially in the admin panel. Since many WordPress core functionality may depend on the version. Also, other plugin may depend on the jQuery version added in the core.

If you are sure that you want to change the core jQuery version, in that case you may add the following CODE in your active theme's functions.php file (even better if you create a plugin for this):

function replace_core_jquery_version() {
    wp_deregister_script( 'jquery' );
    // Change the URL if you want to load a local copy of jQuery from your own server.
    wp_register_script( 'jquery', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' );
}
add_action( 'wp_enqueue_scripts', 'replace_core_jquery_version' );

This will replace core jQuery version and instead load version 3.1.1 from Google's server.

Also, although not recommended, you may use the following additional line of CODE to replace the jQuery version in wp-admin as well:

add_action( 'admin_enqueue_scripts', 'replace_core_jquery_version' );

This way, even after updating WordPress, you'll have the version of jQuery as you want.

A slightly better function:

The replace_core_jquery_version function above also removes jquery-migrate script added by WordPress core. This is reasonable, because the newest version of jQuery will not work properly with an older version of jquery-migrate. However, you can include a newer version of jquery-migrate as well. In that case, use the following function instead:

function replace_core_jquery_version() {
    wp_deregister_script( 'jquery-core' );
    wp_register_script( 'jquery-core', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' );
    wp_deregister_script( 'jquery-migrate' );
    wp_register_script( 'jquery-migrate', "https://code.jquery.com/jquery-migrate-3.0.0.min.js", array(), '3.0.0' );
}
  • Can you just remove the action from functions.php if you find that it breaks your theme? Will it revert back to the original jQuery version or is this a permanent change? – Nick Aug 23 '18 at 16:54
  • 1
    If the callback function of the wp_enqueue_scripts action only updates jQuery and jQuery is being enqueued from somewhere else, then removing the action will restore original jQuery. However, sometimes browsers cache old CODE, depending on server cache settings, So, you'll need to clear browser cache after you do that to see the change. – Fayaz Aug 23 '18 at 17:37
  • ok thanks i just wanted to make sure i didn't screw up my site before adding this action. I had a feeling what you said is accurate but I wanted to play it safe. – Nick Aug 23 '18 at 17:39
  • 1
    This is obviously not permanent, as this change depends on the CODE itself, nothing is saved on the database. So removing the related CODE will revert back to old the state. – Fayaz Aug 23 '18 at 17:39
  • The migrate script for 3.x versions does not work when plugins/themes expect a version below 1.12. More about that here: wordpress.stackexchange.com/a/244543/75495 – cjbj Oct 24 '18 at 13:55

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.