1

I tried all the solutions I found on the internet, including the ones I found here, but nothing works on my theme - jQuery always load in header. I try to use the one from Google, but I would be happy to use the local one too, only in footer. This is what I have now:

   function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js?ver=1.10.2', false, '1.10.2', true);
        wp_enqueue_script('jquery');

        // load a JS file from my theme: js/theme.js
        //wp_enqueue_script('my_script', get_bloginfo('template_url') . '/js/theme.js', array('jquery'), '1.0', true);
    }
}
add_action('init', 'my_init');

Any ideas why no solution works for me?

3
  • 1
    So you have a wp_footer() defined in the footer, right? Feb 25, 2014 at 1:12
  • yes, I do have.
    – vyperlook
    Feb 25, 2014 at 1:39
  • Maybe you have another script loading it via a plugin, or maybe it's hard-coded in the theme.
    – Wyck
    Feb 25, 2014 at 1:59

1 Answer 1

1

You need to load your scripts in the footer so you'll need to add them to the functions.

This method works for parent themes dependent on jQuery.

For child themes

Change:

get_template_directory_uri()

To

get_stylesheet_directory_uri()

Example code for parent themes:

function wpsites_load_scripts() {

wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'wpsites_load_scripts' );

The 5th parameter determines where the script is loaded:

$in_footer

Normally, scripts are placed in of the HTML document. If this parameter is true, the script is placed before the end tag. This requires the theme to have the wp_footer() template tag in the appropriate place.

Default: false

Source http://codex.wordpress.org/Function_Reference/wp_enqueue_script

8
  • sorry, I do not understand how that is related to loading jQuery in the footer. I'm only interested in jQuery.
    – vyperlook
    Feb 25, 2014 at 1:55
  • 2
    The relation is that jQuery cannot load in the footer unless every other script that relies on it also loads in the footer.
    – Otto
    Feb 25, 2014 at 1:58
  • You need to tell WordPress where to load each script using the 5th parameter. The default for $in_footer is false so you need to change that to true for each script in using the wp_enqueue_scripts function so it hooks into wp_footer(). Feb 25, 2014 at 2:01
  • I use minify, with WTC, and I'm loading all the javascrip in footer. The only script that is loading in header is css. is css required to load in the footer too?
    – vyperlook
    Feb 25, 2014 at 2:02
  • I would check that all scripts using jQuery are loaded in the footer and then flush all caching. CSS doesn't use jQuery. Feb 25, 2014 at 2:07

Your Answer

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

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