你的基本策略是正确的。问题是,尽管有一些最佳实践,但你永远无法百分之百地以简单的方式确定:
插件或主题将脚本和样式排队时如果插件或主题确实使用了排队/注册挂钩或硬编码,那么排队的优先级是多少也就是说,您可能想要a)hook your code to different actions when the scripts and styles are printed (和未排队)并测试不同的优先级。或者b)你可以检查每个插件和主题代码,看看每个插件使用了什么挂钩和优先级wp_enqueue_script
或wp_enqueue_style
呼叫
我建议选择选项a,比如:
function wpse_382138_disable_plugins_style()
{
//these should use the same priority of later than the priority used by the plugin or theme when hooked
wp_dequeue_style(\'stylesheet-handler-with-default-priority\');
wp_dequeue_style(\'stylesheet-handler-with-explicit-default-priorit\', 10);
wp_dequeue_style(\'stylesheet-handler-with-later-priority\', 20);
}
function wpse_382138_disable_plugins_scripts()
{
// these should use the same priority of later than the priority used by the plugin or theme when hooked
wp_dequeue_script(\'script-handler-with-default-priority\');
wp_dequeue_script(\'script-handler-with-explicit-default-priority\', 10);
wp_dequeue_script(\'script-handler-with-later-priority\', 20);
}
// option A: hook your unhooking functions either to hooks later than wp_enqueue_scripts or wp_enqueue_style as wp_head or wp_footer.
add_action(\'wp_head\', \'wpse_382138_disable_plugins_style\', 10);
add_action(\'wp_footer\', \'wpse_382138_disable_plugins_style\', 10);
add_action(\'wp_head\', \'wpse_382138_disable_plugins_scripts\', 10);
add_action(\'wp_footer\', \'wpse_382138_disable_plugins_scripts\', 10);
// option b: hook your unhooking functions to the printing actions with a priority lower than the before first scripts or styles are usuarlly written, so they will execute the dequeue before scripts and styles are printed.
add_action(\'wp_print_scripts\', \'wpse_382138_disable_plugins_style\', 5);
add_action(\'wp_print_footer_scripts\', \'wpse_382138_disable_plugins_style\', 5);
add_action(\'wp_print_styles\', \'wpse_382138_isable_plugins_scrips\', 5);
This Codex page 有一个典型WordPress请求中触发的几乎所有挂钩的列表。
如果对你有帮助,请告诉我。