force enqueue script to be first in order of prominence

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)

Read More

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

Thanks very much

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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');