1

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!

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
pjldesign
  • 387
  • 1
  • 3
  • 17

1 Answers1

0

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.

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
  • Thanks but I want the script to appear in the footer, after the plugin's script which currently persists as the last script called in the footer. – pjldesign Aug 02 '19 at 19:46
  • @pjldesign By which name the plugin is enqueuing the `script`? – Sami Ahmed Siddiqui Aug 02 '19 at 20:33
  • @pjldesign Just add the name of the script in the dependency so your script will be loaded after loading the plugin script. You can do this like `array( 'jquery', 'royalslider' )`. Replace `royalslider` with the script name. – Sami Ahmed Siddiqui Aug 02 '19 at 20:35
  • Ok but how do I do that if the plugin's script isn't enqueued or registered and therefore doesn't have a handle? – pjldesign Aug 02 '19 at 20:47
  • Got it. Discovered a function that adds handles to all scripts here: https://cameronjonesweb.com.au/blog/how-to-find-out-the-handle-for-enqueued-wordpress-scripts/ – pjldesign Aug 02 '19 at 21:45