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 am authoring a WP plugin, and I have to generate some JS (jQuery) code in "realtime". It is working fine in general, though I've stuck with one problem with the WP 3.5.1 and its default Twenty Twelve theme: there the scripts are inserted in the footer of the page, not in the header, causing my code to throw errors.

I've looked through all the theme's files (header, footer, functions), and I can't seem to find the reason for that. Looks like the wp_footer() is inserting the block, but I can't find any hook or anything else that tells WordPress to insert scripts there.

So, my question is: how to make jQuery and other scripts go in the header by default, not in the footer? What does make the theme work this way (maybe the default WP behavior changed?)

share|improve this question
1  
Can you show some of the code you've written? Instead of attaching to the wp_footer hook you could attach script blocks to the wp_head hook. –  helgatheviking Mar 3 '13 at 17:30
    
hey, the generated script is inserted in the content and not inserted by the hook, the problem is that it contains jQuery calls and therefore jQuery should be defined earlier. Wyck there answered my question. :) –  Alexander Gilmanov Mar 3 '13 at 21:53
add comment

1 Answer 1

up vote 0 down vote accepted

By default when you use wp_enqueue_script the script is inserted into the <head> section. This is usually set in functions.php.

The argument also has a footer option called $in_footer, if you set this to true, the scripts will instead be added to the footer section. In TwentyTwelve there are several calls to wp_enqueue_script with footer set to true.

For example:

wp_enqueue_script( 
                  'twentytwelve-navigation', 
                   get_template_directory_uri() . '/js/navigation.js', 
                   array(), 
                  '1.0', 
                   true 
                 );

To remove the footer calls you can wp_dequeue_script it, then requeue it for the header or just add false for the 5th parameter. http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Ref:

   wp_enqueue_script( 
         $handle
        ,$src
        ,$deps
        ,$ver
        ,$in_footer //this
    );
share|improve this answer
    
Thank you! That's the thing :) Should have look at this at the beginning, but just didn't think that can be so simple. –  Alexander Gilmanov Mar 3 '13 at 21:53
add comment

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.