0

Hi I am trying to enqueue bootstrap javascript file. But it depends on JQuery and Popper.js. So, I am loading PopperJS from CDN and I am mentioning both JQuery and PopperJS as dependencies on the Bootstrap enqueue. I am not enqueueing JQuery because if learned that Wordpress loads it if you just mention it as a dependency. Like the following:-

wp_enqueue_script( 'popper', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js', array(), null, true );
wp_enqueue_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js', array('jquery', 'popper'), null, true );

However, when I view the source code of my Wordpress site, I see the JQuery version loaded is 1.12.4. The current stable version is 3.2.1. So, how do I rectify this problem. Should I do the unrecommended thing of saving JQuery file to server and enqueueing it separately?

2

Its made like that for backward browser compatibility, if you want the latest version of Jquery, you will need to deregister the script and register it with a new source, you can use a CDN or save the file like you say, remember that the jquery script is just an alias to load, jquery-core and jquery-migrate:

object(_WP_Dependency)#329 (6) {
  ["handle"]=>
  string(6) "jquery"
  ["src"]=>
  bool(false)
  ["deps"]=>
  array(2) {
    [0]=>
    string(11) "jquery-core"
    [1]=>
    string(14) "jquery-migrate"
  }
  ["ver"]=>
  string(6) "1.12.4"
  ["args"]=>
  NULL
  ["extra"]=>
  array(0) {
  }
}

so you have to:

wp_deregister_script('jquery'); //because its a Core-Registered Script
wp_register_script('jquery', 'THE PATH TO THE FILE', array(), '', FALSE);

and this will be compatible with any plugin/theme that is using jquery or jquery-core as a dependency.

  • after registering it, do I have to enqueue it as well? – Grey Lover Nov 2 '17 at 4:50
  • it depends, if its a dependency in another script that is being queued using wp_enqueue_script wordpress will already put the jquery lib in the queue array so it will load as a dependency as usual. – David Lee Nov 2 '17 at 5:52
1

WP core uses an older version of jQuery for the purposes of browser compatibility, since it is part of its mandate to support older browsers.

There are many ways you could include jQuery in your WP project and manage its version. The most straightforward would be to enqueue the script as you have suggested and update the script manually as needed, but you could without much difficulty set up a package manager like Bower or npm Browserify to automate the versioning process. See for example https://generatewp.com/automation-wordpress-practical-guide/

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.