移除 WordPress5.9.2前台的内联样式和Svg图像

240 阅读1分钟

最近将 WordPress 升级到了5.9.2,然后在查看源代码时发现 wordpress在头部插入了global-styles 内联样式,并向底部插入了很多的 svg 图像,对于国内主题的话,这个些代码显得有些多余了,因此决定把这些去掉。

移除头部的global-styles内联样式

在主题的functios.php添加

 function remove_global_styles(){
 wp_dequeue_style( 'global-styles' );
 }
 add_action( 'wp_enqueue_scripts', 'remove_global_styles' );

移除底部的svg图像

在使用主题的根目录下新建一个theme.json文件,写入下面代码

 {
    "version": 1,
   "settings": {
>      "color": {
      "duotone": null
     }
   }
 }

一键移除内联样式和svg图像

 function remove_global_styles(){
   remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles');
   remove_action('wp_footer', 'wp_enqueue_global_styles', 1);
 }
 add_action('after_setup_theme', 'remove_global_styles', 10, 0);

移除所有区块样式

在主题的functions.php中添加

 function remove_wp_block_library_css(){
   wp_dequeue_style( 'wp-block-library' );
   wp_dequeue_style( 'wp-block-library-theme' );
   wp_dequeue_style( 'wc-block-style' ); // 移除WOO插件区块样式
   wp_dequeue_style( 'global-styles' ); // 移除 THEME.JSON
 }
 add_action( 'wp_enqueue_scripts', 'remove_wp_block_library_css', 100 );
 }

欢迎关注我的公众号“xx主题网”,原创技术文章第一时间推送。

主题网二维码.jpg

文章来源:www.xxzhuti.com/929.html