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 ?

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

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.

  • 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 '16 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 '16 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 '16 at 7:34
  • Remove https: leave only //Ajax...., The function will detect the right protocol – Benoti Dec 10 '16 at 7:36
  • Does that have to do anything with my question !? – user108685 Dec 10 '16 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy