9

I have a few scripts which are being enqueued,

the problem is that I want to force the order of prominece to which these scripts are loaded. There is one in particular which is loaded from a plugin before the theme ones which requires jquery however the plugin does not require jquery (bad dev on the plugin but I'd rather not touch 3rd party code for futureproofing reasons)

is there some way to mess with the enqueue order at runtime?

Thanks very much

2 Answers 2

15

You just need to enqueue your scripts before plugin does it. You can do it by setting priority to 0 for your hook. For example, do the following:

add_filter( 'wp_enqueue_scripts', 'wpse8170_enqueue_my_scripts', 0 );
// or if you enqueue your scripts on init action
// add_action( 'init', 'wpse8170_enqueue_my_scripts', 0 );

function wpse8170_enqueue_my_scripts() {
    wp_enqueue_script( 'myscript', 'http://path/to/my/script.js', array( 'jquery' ) );
    // my else scripts go here...
}

Setting up priority for your hooks will put it to the beginning of calling queue and your scripts will be added first.

3
  • sorry to be naive but i take it that wpse8170_enqueue_my_scripts_on_init is a custom function with standard enqueue_scripts call in it?
    – Alex
    Commented Jun 13, 2012 at 8:51
  • @Alex i have updated my answer, check it out. Commented Jun 13, 2012 at 9:01
  • 1
    This doesn't always work: wordpress.stackexchange.com/questions/91961/…
    – Dave Kiss
    Commented May 23, 2013 at 16:44
1

To add script before other scripts, use wp_register_script() instead of wp_enqueue_script(), and then manually add your script to be first at queue:

wp_register_script('handle', get_template_directory_uri()."/custom.js");
array_unshift(wp_scripts()->queue, 'handle');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.