Currently migrating from an older version of a JS library (sweetAlerts), to a newer version.
Both versions load, but I want to be able to use the updated library, but unsure how to reference it in JavaScript to use the newer library. Any input on how to do this would be appreciated.
wp_enqueue_script( 'sweet-alert', plugin_dir_url( __FILE__ ) . 'js/sweetalert.min.js', array(), $this->version, false );
wp_enqueue_script( 'sweet-alert-latest', plugin_dir_url( __FILE__ ) . 'js/sweetalert-latest.min.js', array(), $this->version, false );
Currently migrating from an older version of a JS library (sweetAlerts), to a newer version.
Both versions load, but I want to be able to use the updated library, but unsure how to reference it in JavaScript to use the newer library. Any input on how to do this would be appreciated.
wp_enqueue_script( 'sweet-alert', plugin_dir_url( __FILE__ ) . 'js/sweetalert.min.js', array(), $this->version, false );
wp_enqueue_script( 'sweet-alert-latest', plugin_dir_url( __FILE__ ) . 'js/sweetalert-latest.min.js', array(), $this->version, false );
Share
Improve this question
asked Apr 11, 2019 at 13:51
Moo MasterMoo Master
33 bronze badges
2 Answers
Reset to default 0sweetalert.js uses the global swal
in both version 1.*.* and version 2.*.* of its library. Therefore if you load both js/sweetalert.min.js
and js/sweetalert-latest.min.js
the first one to define the global swal
will be used, the second will be ignored or cause a console error.
To prevent this only load one library such as:
// PHP
wp_enqueue_script( 'sweet-alert-latest', plugin_dir_url( __FILE__ ) . 'js/sweetalert-latest.min.js', array(), $this->version, false );
And update any references targeting old SweetAlert code according to the migration documentation
Alternatively If you want to use both libraries concurrently you could utilise ES6 modules with a compiler such as babel
// Javascript
import swal as oldswal from '/my-local-js-directory/sweetalert.1.5.1.js';
import swal as newswal from '/my-local-js-directory/sweetalert.2.1.0.js';
// Old alert
oldswal({
// Old alert config here
});
// New alert
newswal({
// New alert config here
});
Ref: Babel
With common JS you could do something like this
const oldswal = require('/my-local-js-directory/sweetalert.1.5.1.js')
const newswal = require('/my-local-js-directory/sweetalert.2.1.0.js')
Looks like this is not a WordPress related question but external JS library.
Anyway, if for this case you can load both JS files and loading the library twice has no conflicts, I would use the dependency
, version
and in_footer
parameters for the wp_enqueue_script function as the following:
wp_enqueue_script( 'sweet-alert', plugin_dir_url( __FILE__ ) . 'js/sweetalert.min.js', array(), '1.0', true );
wp_enqueue_script( 'sweet-alert-latest', plugin_dir_url( __FILE__ ) . 'js/sweetalert-latest.min.js', array('sweet-alert'), '2.0', true );
Note that I'm not using the class version ($this->version
).
This way you are telling WordPress to load v 1.0 first and v 2.0 later, also is a good practice for page performance to load JS on footer, unless you are already handling that in some other way.
I hope this can help.