0

I have added this code to load Google hosted jQuery

function replace_jquery() {
    if (!is_admin()) {
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' );
        wp_enqueue_script('jquery');
    }
}
add_action('wp_footer', 'replace_jquery', 0 ); 

A plugin requires jQuery and it is placed in wp_footer with priority value of 19. So I want to add jQuery before that. As you can see, I have given the above code a priority of 0. But when in the output HTML, the jQuery link is added in the last. My guess is that because wp_enqueue_script has default priority of 20. If I am right, How do I over ride to load it first ?

1 Answer 1

1

To properly enqueue jQuery, it's usually done with wp_enqueue_scripts, there's no need to load it from cdn as jQuery library are provides in the wp-includes folder of WordPress.

Only wp_enqueue_script('jquery'); is required.

 add_action('wp_enqueue_scripts', 'replace_jquery');

If you really want to replace jQuery with the cdn link, it's better to register and enqueue it with this function.

8
  • The problem is it is loaded in the head of the document. It becomes render blocking and slows down the page. two of my plugins need jQuery but both are added in the footer. So it is enough if I add jQuery in the footer. So what i'm asking is, Is there a way to control the loading 'wp_enqueue_script' so that I could load it with first priority in the 'footer'
    – user108685
    Dec 10, 2016 at 7:13
  • Did you try to make it work like this ? I'm only talking about jQuery library not your plugin script that can be add to the footer. I never saw jQuery enqueue in the footer. The blocking and slows comes from the cdn load, if the jQuery version doesn't fit to you, copy and load it from your theme or plugin.
    – Benoti
    Dec 10, 2016 at 7:20
  • Yes, I also tried adding the local version. The above method you said loads in the head of the page. Page speed tool says that it is render blocking. It doesn't matter which version of jQuery is added, I just want to add it in a non-render blocking way.
    – user108685
    Dec 10, 2016 at 7:34
  • Remove https: leave only //Ajax...., The function will detect the right protocol
    – Benoti
    Dec 10, 2016 at 7:36
  • Does that have to do anything with my question !?
    – user108685
    Dec 10, 2016 at 7:40

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.