Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
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?

Thanks.

share|improve this question

1 Answer 1

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

function load_my_script(){
    wp_register_script( 
        '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.

share|improve this answer
    
Awesome! Thanks! –  Dave Apr 10 '12 at 17:30
    
... 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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