ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在wordpress中替换排队脚本的正确方法?

2019-11-22 13:31:04  阅读:284  来源: 互联网

标签:jquery-ui wordpress-plugin wordpress php jquery


我需要从wordpress中删除jquery ui脚本(和其他一些),问题是如果我出队并注销了它们,则调用它们的插件不起作用,因为这些脚本“丢失了”,即使我用通过googleapis链接创建一个脚本.

我有以下几点:

add_action('wp_print_scripts','remove_excess_scripts', 100);
function remove_excess_scripts(){
  wp_dequeue_script( 'jquery-ui-core' );
  wp_dequeue_script( 'jquery-ui-widget' );
  wp_dequeue_script( 'jquery-ui-mouse' );
  wp_dequeue_script( 'jquery-ui-accordion' );
  wp_dequeue_script( 'jquery-ui-autocomplete' );
  wp_dequeue_script( 'jquery-ui-slider' );
  wp_dequeue_script( 'jquery-ui-progressbar' );
  wp_dequeue_script( 'jquery-ui-tabs' );
  wp_dequeue_script( 'jquery-ui-sortable' );
  wp_dequeue_script( 'jquery-ui-draggable' );
  wp_dequeue_script( 'jquery-ui-droppable' );
  wp_dequeue_script( 'jquery-ui-selectable' );
  wp_dequeue_script( 'jquery-ui-position' );
  wp_dequeue_script( 'jquery-ui-datepicker' );
  wp_dequeue_script( 'jquery-ui-tooltip' );
  wp_dequeue_script( 'jquery-ui-resizable' );
  wp_dequeue_script( 'jquery-ui-dialog' );
  wp_dequeue_script( 'jquery-ui-button' );

  wp_deregister_script( 'jquery-ui-core' );
  wp_deregister_script( 'jquery-ui-widget' );
  wp_deregister_script( 'jquery-ui-mouse' );
  wp_deregister_script( 'jquery-ui-accordion' );
  wp_deregister_script( 'jquery-ui-autocomplete' );
  wp_deregister_script( 'jquery-ui-slider' );
  wp_deregister_script( 'jquery-ui-progressbar' );
  wp_deregister_script( 'jquery-ui-tabs' );
  wp_deregister_script( 'jquery-ui-sortable' );
  wp_deregister_script( 'jquery-ui-draggable' );
  wp_deregister_script( 'jquery-ui-droppable' );
  wp_deregister_script( 'jquery-ui-selectable' );
  wp_deregister_script( 'jquery-ui-position' );
  wp_deregister_script( 'jquery-ui-datepicker' );
  wp_deregister_script( 'jquery-ui-tooltip' );
  wp_deregister_script( 'jquery-ui-resizable' );
  wp_deregister_script( 'jquery-ui-dialog' );
  wp_deregister_script( 'jquery-ui-button' );
}

加上前面的功能,我正确注册了jQuery ui并使其入队:

wp_register_script(
        'jquery-ui-all',
        "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
        array('jquery'),
        false,
        false
    );

function print_scripts() {
wp_enqueue_script( 'the-bootstrap' );   
}
add_action( 'wp_enqueue_scripts', 'print_scripts' );

(其中-bootstrap具有jquery-ui-all的依赖项)

所有这些工作都可以从Google加载jquery-ui且不再输出任何单独的UI文件-出现问题是因为插件WPFullCalendar将一堆jquery ui单个脚本作为依赖项使其脚本排入队列,我猜与他们注销并出队,其脚本无法获取输出,因为缺少依赖项?我收到以下错误(由于未输出其JS文件)

Uncaught ReferenceError: WPFC is not defined 

如何替换脚本,以便其他插件仍然可以排队或依赖它们,但是它们都输出相同的文件.我可以通过注销所有名称,然后使用googleapis作为源再次注册它们来实现,但是对于所有相同的googleapi链接,我仍然会收到15个不同的HTTP请求…我需要所有这些文件都指向1 googleapi文件,仅用于输出.这可能吗? (在已经成功调用/依赖于其他插件的其他插件之后,不应将它们从队列中出队-wp_print_scripts在所有插件入队后发生,不是吗?)

更新:

我对下面接受的答案进行了修改,使其更加具体,以免破坏依赖关系.这是更新代码,可正确注销脚本并从所有依赖的脚本中删除对脚本的依赖.确保执行此操作,用于替换注销的脚本的HTML(可能是head)加载得足够高,以便其他依赖于此的脚本随之出现.

//custom deregister scripts
function georgian_deregister_scripts(){
    global $wp_scripts;
    //scripts to deregister
    $deregisteredscripts = array('jquery-ui-core','jquery-ui-widget','jquery-ui-mouse','jquery-ui-accordion','jquery-ui-autocomplete','jquery-ui-slider','jquery-ui-progressbar' ,'jquery-ui-tabs','jquery-ui-sortable','jquery-ui-draggable','jquery-ui-droppable','jquery-ui-selectable','jquery-ui-position','jquery-ui-datepicker','jquery-ui-tooltip','jquery-ui-resizable','jquery-ui-dialog','jquery-ui-button');
    //degregister each script
    foreach($deregisteredscripts as $script)
      wp_deregister_script( $script );
    //remove deregistered scripts as dependencies of any other scripts depending on them  
    if(false != $wp_scripts->queue)
      foreach($wp_scripts->queue as $script)
        if(isset($wp_scripts->registered[$script]))
          $wp_scripts->registered[$script]->deps = array_diff($wp_scripts->registered[$script]->deps, $deregisteredscripts);
}
add_action('wp_enqueue_scripts', 'georgian_deregister_scripts', 101);

解决方法:

如果取消注册脚本,则该脚本不会排队,因此您有几个无用的命令.

现在,如何删除对要入队的(所有)脚本的依赖?此功能可以解决问题.

function customize_scripts_output(){
    global $wp_scripts;

    if(false != $wp_scripts->queue)
        foreach($wp_scripts->queue as $script)
            if(isset($wp_scripts->registered[$script]))
                $wp_scripts->registered[$script]->deps = array();
}
add_action('wp_enqueue_scripts', 'customize_scripts_output', 101);

标签:jquery-ui,wordpress-plugin,wordpress,php,jquery
来源: https://codeday.me/bug/20191122/2059982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有