function load_my_script(){
wp_register_script( 'my_script', get_template_directory_uri() . '/js/myscript.js', 'jquery' );
wp_enqueue_script( 'my_script', 'jquery');
add_action('wp_enqueue_scripts', 'load_my_script');

myscript.js is loading before jquery, why? And how do I make it load AFTER jquery?



You have a typo in your code. It should be:

function load_my_script(){
        get_template_directory_uri() . '/js/myscript.js', 
        array( 'jquery' )
    wp_enqueue_script( 'my_script' );
add_action('wp_enqueue_scripts', 'load_my_script');

The jQuery dependency needs to be an array(), not just a string. This will force your script to load after jQuery.

  • ... is 'jquery' needed in wp_enqueue_script? – Dave Apr 10 '12 at 17:41
  • In the wp_register_script function, yes. It tells WordPress that jQuery is needed for this script to run, so it'll be loaded accordingly. In wp_enqueue_script it's not necessary, because WP already knows the dependencies from before. – swissspidy Apr 10 '12 at 17:56
  • Exactly. Sorry, forgot to remove jQuery from the wp_enqueue_script() call. It's not needed there. – EAMann Apr 10 '12 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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