首页 > 解决方案 > 在后端删除 Gutenberg 编辑器的嵌入式样式表

问题描述

Gutenberg 编辑器带有一个嵌入式样式表。这是该样式表的一个片段:

...

.editor-styles-wrapper {
  font-family: "Noto Serif", serif;
  font-size: 16px;
  line-height: 1.8;
  color: #191e23;
}

.editor-styles-wrapper p {
  font-size: 16px;
  line-height: 1.8;
}

...

我使用以下内容将自己的编辑器样式表排入队列:

add_action("enqueue_block_editor_assets", "enqueue_custom_block_editor_assets");
function enqueue_custom_block_editor_assets() {
  wp_enqueue_style("editor-style", get_stylesheet_directory_uri()."/editor-style.css", null, null);
}

由于我有自己的编辑器 stylehseet,我想摆脱默认的。对这个主题的搜索会产生很多删除前端默认块样式的结果,但我指的是后端的编辑器样式。谢谢你的帮助!

标签: wordpresswordpress-gutenberg

解决方案


我深入研究了它是如何被注入的,并找到了一种通过block_editor_settings过滤器将其消灭的方法。有一个styles包含 css 的数组的键。对我来说唯一不确定的是我假设数组的形状总是相同的,我应该在这里做一些检查。

add_filter( 'block_editor_settings' , 'remove_guten_wrapper_styles' );
public function remove_guten_wrapper_styles( $settings ) {
    unset($settings['styles'][0]);

    return $settings;
}

推荐阅读