douguo7431 2017-07-17 13: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 13: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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 目前wxid加好友的渠道都被和谐了吗
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出