wp_enqueue_script : how to change loading order of scripts?

When you take a look at the source of wp_enqueue_scripts() (or the register-sister), then you’ll see that there’s the global $wp_scripts handling all the heavy stuff.

Basically the global is just an instance of WP_Scripts, which is a child of WP_Dependency and you can use all the magic from there if there’s no higher level API available.

To see all registered, enqueued, etc. scripts, simply

var_dump( $GLOBALS['wp_scripts']->registered );

or do the same with enqueue. Luckily you got one argument to actually sort your stuff: dependencies. So if you want to be one script loaded after jQuery has been loaded, simply add a dependency of array( 'jquery' ) when registering or enqueueing your script. That’s the way you order them in WordPress: Make them dependent on each other. Btw, the dependency name always simply is the name it was registered with, called the “handle”.

Leave a Comment