douguo7431 2017-07-17 05:15
浏览 105
已采纳

在WordPress函数文件中添加jQuery

I have been having a problem where my contact form 7 plugin has been interfering with my jQuery for slick slider. I think its because the contact form 7 old jQuery is overriding mine. I originally added the query with in the head. I think if I do it through the functions file it might fix this. I found this code which solved my problem but I'm not sure what its doing exactly.

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}  
  • 写回答

1条回答 默认 最新

  • douqiao1997 2017-07-17 05:36
    关注

    Basically, what the snippet is doing is changing the already registered and enqueued jquery with the newer file of your choice (in this case, loading jquery from google cdn).

    Lets break it down:

    if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
    

    If you're not logged in as admin, then you attach the function "my_jquery_enqueue" to the hook "wp_enqueue_scripts" with priority 11.

    The function my_jquery_enqueue() then removes the registered jquery script (probably registered by another plugin with the handler jquery), registeres the script you want to have there and finally enqueues it.

    All this happens before WP generates the page (that's why you can swap out the file easily). Have a read through the following links to understand it better:

    1. Plugin API, hooks, Actions and Filters
    2. wp_enqueue_scripts
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部