最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to load jQuery in TOP of wp_footer?

programmeradmin1浏览0评论

I have added this code to load Google hosted jQuery

function replace_jquery() {
    if (!is_admin()) {
        wp_register_script('jquery', '.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 ?

I have added this code to load Google hosted jQuery

function replace_jquery() {
    if (!is_admin()) {
        wp_register_script('jquery', 'https://ajax.googleapis/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 ?

Share Improve this question asked Dec 9, 2016 at 19:35 user108685user108685
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论