1

I understand that this line of code (placing in the header.php) adds jquery:

<?php wp_enqueue_script("jquery"); ?>

This loads it in the header section though...

So, I just added it to the footer.php and it works fine...

Can someone just let me know if this is the accepted way of loading jquery in the footer?

CONTENT CONTENT CONTENT

</footer>

<?php wp_enqueue_script("jquery"); ?>

<?php wp_footer(); ?>

</body>
</html>
1
  • I'd avoid attempting to load jQuery in the footer at all. Other plugins expect it in the header, and will therefore load it in the header because they have set it as a dependency, and jQuery is automatically registered to the header. To load it in the footer you'd need to de-register jQuery and re-register it in the footer, but you still risk compatibility issues. Aug 15, 2017 at 10:54

1 Answer 1

2

You shouldn't enqueue jQuery by manually using wp_enqueue_script('jquery');. WordPress can enqueue it for you the right way. All you have to do is to declare jQuery as your script's dependency:

wp_enqueue_script(
    'my-script', 
    get_template_directory_uri() .'/my-script.js', 
    array('jquery'), 
    null, 
    true
);

You can pass an array of dependencies as the 3rd parameter. Also, by setting the 5th parameter to true, you ask WordPress to enqueue this script in the footer.

If you really insist on moving jQuery to the footer, there are some optimization plugins such Autoptimize that can do this for you.

If you still really really would like a continuance, you can do so as follows:

function move_jquery_to_footer() {

    wp_deregister_script('jquery');
    wp_enqueue_script(
        'jquery', 
        'includes_url( '/js/jquery/jquery.js' )', 
        array(), 
        null, 
        true
   );

}
add_action('wp_enqueue_scripts', 'move_jquery_to_footer');

This is not recommended, and doing so can disqualify your theme or plugin in a lot of repositories and markets.

You can also defer parsing of your scripts. This way, your scripts will be parsed asynchronously. ( Code by @toscho from this answer )

add_filter( 'script_loader_tag', function ( $tag, $handle ) {

    return str_replace( ' src', ' defer="defer" src', $tag );

}, 10, 2 );
4
  • What about speed? I thought placing at the bottom/ footer makes the site load faster?
    – Henry
    Aug 15, 2017 at 10:57
  • Instead of that, you can defer parsing of your script. In that case, it doesn't matter where it's placed, since it's being loaded asynchronously. The reason of not moving jQuery to footer is that because some plugins are not coded properly, and if you move jQuery in the footer, it has the potential to break them.
    – Johansson
    Aug 15, 2017 at 11:00
  • @henry It could be faster, but you're dealing with an existing system that made a decision a long time ago that you need to deal with and adapt to. If you're site is very locked down and you know every plugin and piece of code that's running on it, then you might be ok making this optimisation. Aug 15, 2017 at 11:12
  • 1
    Thanks for all answers....it seems better to keep it where it is expected I guess...
    – Henry
    Aug 15, 2017 at 11:18

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.