1

I wanted to move jquery to the site's footer, but that doesn't work. I've tried it with the following code.

As you can see, I have a couple of another scripts and they load correctly on the footer. Only jquery won't work.

//This adds our JavaScript Files
function wel_adding_scripts() {

wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', '', '2.1.1', true); 
wp_enqueue_script( 'jquery');

wp_register_script('jquery.mixitup', get_template_directory_uri() . 'js/jquery.mixitup.min.js','','1.1', true);
wp_enqueue_script('jquery.mixitup');

wp_register_script('headroom', get_template_directory_uri() . 'js/headroom.min.js','','1.1', true);
wp_enqueue_script('headroom');
}
add_action( 'wp_enqueue_scripts', 'wel_adding_scripts' ); 
0

4 Answers 4

6

Note that if any script in the header at all needs jQuery then it will bump it to header, regardless if it's changed to be enqueued for footer.

So while changing it to be queued for footer makes sense it rarely works out when a lot of script stuff is going on.

1
  • That's a great point!
    – Tomás Cot
    Aug 19, 2014 at 16:17
1

Here is the problem

If you try to register or enqueue an already registered handle with different parameters, the new parameters will be ignored. Instead, use wp_deregister_script() and register the script again with the new parameters.

You will have to deregister jquery first, the register it again, but becareful because the empty string isn't what you should put in the place of the deps parameter. IT should be an empty array.

Like this:

     wp_register_script('jquery',
 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js',
     array() , '2.1.1', true); 
7
  • Hi, i have deregistered it as you cann see it at the code. Now i have put empty array() and still no changes.
    – hazelnut
    Aug 19, 2014 at 12:37
  • Also, change the priority in the add_action , I think it should be 11, but read the docs if that doesn't work.
    – Tomás Cot
    Aug 19, 2014 at 12:45
  • yeah, but what shall i read? I've add the priority 11 and it still does not work. I have read many Tutorials about this and i also read the wp_register_scripts documentation. I can't figure it out, why it won't work.
    – hazelnut
    Aug 19, 2014 at 12:52
  • Are you using a caching plugin?
    – Tomás Cot
    Aug 19, 2014 at 12:58
  • No, this is a clean WordPress only for Theme Testing.
    – hazelnut
    Aug 19, 2014 at 13:10
0

Fixed: I have do it the right way, all the time. Now i found, that jquery was deregistered at the themes header.php file. I have deleted it and it works now.

-1
<?php
function your_function() {
    echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', 'my_function');
?>

You can use this hook. This is used to load the script in the footer try it let me know for any doubts.

3
  • Don't use wp_footer to load scripts. If you properly enqueue/register a script and use the wp_enqueue_scripts hook properly, which is the correct hook that you should use your scripts should be loaded in the footer Aug 19, 2014 at 12:55
  • @pieter Goosen I didn't get you could you explain me clearly why we want to use wp_wnqueue_scripts?
    – Sri
    Aug 19, 2014 at 13:13
  • Read here :-) Aug 19, 2014 at 13:18

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.