I have an initialization script for a slider which must load after the slider's script is loaded, however, the slider script is being loaded by a plugin and persists at the end of the script calls regardless of me prioritizing my initialization script 999999 in the enqueue action. How do I get it to load after the plugin script?
In my child theme's functions.php
:
function init_royalslider() {
wp_enqueue_script(
'sliderInit',
get_stylesheet_directory_uri() . '/template-parts/royalslider-custom/js/sliderInit.js',
array( 'jquery' ), '', true
);
}
add_action( 'wp_enqueue_scripts', 'init_royalslider', 9999999 );
Thanks for any insight!
Try this:
function init_royalslider() {
wp_enqueue_script(
'sliderInit',
get_stylesheet_directory_uri() . '/template-parts/royalslider-custom/js/sliderInit.js',
array( 'jquery', 'royalslider' ), '', false
);
}
add_action( 'wp_enqueue_scripts', 'init_royalslider', 10 );
Pass false
in the last parameter. If you pass true
that means you saying to add the script in the footer as shown here: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
EDITED: Replace royalslider
with the plugin script name. As you are telling WordPress that your script depends on the jQuery
similary you can add the another script name. In this way, WordPress add the script after the dependent script. Please refer wp_enqueue_script documentation for further details.