【发布时间】:2021-04-10 03:20:50
【问题描述】:
我正在尝试 dequeue/deregister 我的 wordpress 网站的所有帖子/页面中不必要的样式(WooCommerce,联系表格 7)。
尝试officalWooCommerce 禁用这些额外样式和脚本的方法,但由于样式仍加载到源代码中且 Google PageSpeed Insights 仍将它们显示为正在渲染,因此无法将它们出列;所以:
add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
return $enqueue_styles;
}
// Or just remove them all in one line
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
在functions.php 中不起作用。我也尝试像这样删除它们:
function remove_assets() {
wp_dequeue_style('wc-block-vendors-style-css');
wp_deregister_style('wc-block-vendors-style-css');
}
add_action( 'wp_print_styles', 'remove_assets', PHP_INT_MAX );
指向特定样式 id wc-block-vendors-style-css 并设置 PHP_INT_MAX。
它仍然没有删除那个特定的样式。
注意事项:
这个functions.php属于一个自定义的子主题,重做了二十二十个WordPress主题,缓存已被清除。我已经尝试了 StackOverflow 中用户遇到类似问题的大部分答案,但没有一个有效。
主题中启用了 Woocommerce 支持。
如何在 2021 年使用 WordPress 5.6 使样式和脚本出列/取消注册?
【问题讨论】:
-
除了提供的答案...您的官方答案使用了不正确的功能。 gist.github.com/woogists/…
-
@HowardE 那 sn-p 是我提出问题的 woocomerce 文档源的一部分,也尝试了那个,但不起作用。所有 WooCommerce 样式和脚本仍在加载
-
要点说
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );你上面写的是__return_false -
docs.woocommerce.com/document/disable-the-default-stylesheet/… 阅读此文;你的建议在那里,试过了,它不起作用..
-
在这种情况下,您应该确保您的函数正在实际运行。您可以尝试将某些内容转储到 error_log 以验证您的函数正在触发。
标签: php css wordpress woocommerce