0

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?)

2
  • 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. Mar 3, 2013 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. :) Mar 3, 2013 at 21:53

1 Answer 1

0

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
    );
1
  • Thank you! That's the thing :) Should have look at this at the beginning, but just didn't think that can be so simple. Mar 3, 2013 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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