如何在WordPress管理面板中取消排队或取消注册所有jquery脚本


How to dequeue or deregister all jquery scripts in wordpress admin panel

我正在开发一个插件,我在插件中使用wordpress color picker

color_picker.js:

jQuery(document).ready(function($){
    jQuery('.cp-field').wpColorPicker();
});

在索引.php文件中:

add_action('admin_init', 'enqueue_color_picker');
function enqueue_color_picker($hook_suffix) {
    // first check that $hook_suffix is appropriate for your admin page
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('cp-script-handle', plugin url.'js/color_picker.js', array( 'wp-color-picker' ), false, true);
}

之后,我刷新我的管理页面并查看源代码,所有 jQuery 和 JQuQuery-UI 脚本在正文标签结束之前加载,如下所示:

<script type='text/javascript' src='http://site_url/wp-admin/load-scripts.php?c=1&amp;load%5B%5D=hoverIntent,common,admin-bar,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-draggable,jquery-ui-slider,jquery-touch-p&amp;load%5B%5D=unch,iris,wp-color-picker,jquery-ui-sortable,svg-painter,heartbeat,wp-auth-check&amp;ver=4.0'></script>
<script type='text/javascript' src='http://site_url/wp-content/plugins/wp_foo/js/color_picker.js?ver=4.0'></script>
<div class="clear"></div></div><!-- wpwrap -->
<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
</body>

当我评论颜色选择器功能时,所有脚本都会消失。

我想卸载或取消排队或取消注册所有不需要的脚本。我也尝试取消排队或取消脚本,但没有任何反应

add_action('admin_init', 'unload_all_jquery');
function unload_all_jquery() {
    //wp_enqueue_script("jquery");
    $jquery_ui = array(
        "jquery-ui-widget",
        "jquery-ui-mouse",
        "jquery-ui-accordion",
        "jquery-ui-autocomplete",
        "jquery-ui-slider",
        "jquery-ui-tabs",   
        "jquery-ui-draggable",
        "jquery-ui-droppable",
        "jquery-ui-selectable",
        "jquery-ui-position",
        "jquery-ui-datepicker",
        "jquery-ui-resizable",
        "jquery-ui-dialog",
        "jquery-ui-button"
    );
    foreach($jquery_ui as $script){
        wp_dequeue_script($script);
    }
}

任何建议我该怎么做。

为了在

后台"取消注册"脚本,您需要挂接到'admin_enqueue_scripts'钩子。这是也用于将管理脚本排队的钩子(顾名思义)。

此外,您需要使用 wp_deregister_script() 而不是 wp_dequeue_script() 。原因是脚本已"注册"到队列,但实际上并未"排队"。因此,您的最终脚本将如下所示:

add_action('admin_enqueue_scripts', 'unload_all_jquery');
function unload_all_jquery() {
    //wp_enqueue_script("jquery");
    $jquery_ui = array(
        "jquery-ui-widget",
        "jquery-ui-mouse",
        "jquery-ui-accordion",
        "jquery-ui-autocomplete",
        "jquery-ui-slider",
        "jquery-ui-tabs",   
        "jquery-ui-draggable",
        "jquery-ui-droppable",
        "jquery-ui-selectable",
        "jquery-ui-position",
        "jquery-ui-datepicker",
        "jquery-ui-resizable",
        "jquery-ui-dialog",
        "jquery-ui-button"
    );
    foreach($jquery_ui as $script){
        wp_deregister_script($script);
    }
}