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.

I've written a plugin which uses wp_enqueue_script("jquery"); to load jQuery. I need to load jQuery in the head and not at the bottom of the body. However, I think that this command is overwritten by some theme which loads jQuery at the bottom of the body so preventing my plugin to work properly. How can I force WP to load jQuery in the head?

share|improve this question
5  
if the location of jquery prevents your plugin from working, you're probably doing something wrong. –  Milo Mar 22 '13 at 1:50
1  
+1 for Milo's comment. To expand on it: I can only speak for myself, but I'd consider it bad practice of a plugin author to force my WP to load jQuery in the <head>. I would venture to guess, that you simply enqueue your own scripts too early, read up on wp_enqueue_script and make them dependent on 'jquery'. –  Johannes Pille Mar 22 '13 at 15:00

1 Answer 1

Edit: new solution, based on one of the comments. If you are using wp_enqueue_script to load your js file, make sure jQuery is a dependency so that it gets called before your code:

wp_enqueue_script( 'my_script', 'my-script.js', array( 'jquery'), $ver, true );

You can then remove the original enqueue call you had since jQuery will be included automagically

share|improve this answer
    
Apart that your command is a syntax error according to the page you cite, if my script fails because jQuery is not loaded, how can it not fail calling jQuery(...) as you suggest? Anyway, it does not work. –  Mark S Mar 22 '13 at 13:59
    
just updated. There are a couple ways of doing it –  Manny Fleurmond Mar 22 '13 at 14:32
    
ReferenceError: jQuery is not defined. And indeed I continue to not understand why jQuery( document ).ready( function($){ should work when jQuery(function (){ fails. If jQuery is NOT loaded yet, any call to jQuery fails. Isn't it? –  Mark S Mar 22 '13 at 14:39
    
The WordPress-bundled jQuery library is configured in no-conflict mode. –  Chip Bennett Mar 22 '13 at 19:10
    
Try to use: var jq = jQuery || $.noConflict() –  balamurugan Jan 13 at 6:37

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.