dee*_*eem 1 php wordpress jquery admin
我正在开发一个插件并且在我正在使用的插件中 wordpress color picker
color_picker.js:
jQuery(document).ready(function($){
jQuery('.cp-field').wpColorPicker();
});
Run Code Online (Sandbox Code Playgroud)
并在 index.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);
}
Run Code Online (Sandbox Code Playgroud)
之后,我刷新我的管理页面并查看源代码,所有 jquery 和 jquery-ui 脚本在 body 标记结束之前加载,如下所示:
<script type='text/javascript' src='http://site_url/wp-admin/load-scripts.php?c=1&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&load%5B%5D=unch,iris,wp-color-picker,jquery-ui-sortable,svg-painter,heartbeat,wp-auth-check&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>
Run Code Online (Sandbox Code Playgroud)
当我评论颜色选择器功能时,所有脚本都会运行。
我想卸载、出列或注销所有不需要的脚本。我也尝试出列或删除脚本,但没有任何反应
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);
}
}
Run Code Online (Sandbox Code Playgroud)
任何建议我怎么能做到这一点。
为了在管理员中“注销”一个脚本,您需要挂入'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);
}
}
Run Code Online (Sandbox Code Playgroud)