admin管理员组文章数量:1125345
I had a site I was running with a block-based/FSE theme on WP v.6.1.1. In functions.php
, I was loading my theme stylesheet in the Gutenberg editor using the following:
function dnl_theme_add_editor_styles() {
add_editor_style();
}
add_action( 'after_setup_theme', 'dnl_theme_add_editor_styles' );
This worked perfectly well (editor-style.css
loaded in Gutenberg) until I upgraded to WordPress 6.2. After that, the stylesheet stopped getting loaded in the editor. I tried loading style.css
using add_editor_style
like so:
function dnl_theme_add_editor_styles() {
add_editor_style( 'style.css' );
}
I also tried calling add_theme_support
first, e.g.:
function dnl_theme_add_editor_styles() {
add_theme_support( 'wp-block-styles' );
add_editor_style();
}
But none of that helped.
Has anyone else noticed this behavior after upgrading? Does anyone know why this would be happening? I looked at the source code for add_editor_style
in wp-includes/theme.php
and didn't see any changes there since 6.1.1.
I had a site I was running with a block-based/FSE theme on WP v.6.1.1. In functions.php
, I was loading my theme stylesheet in the Gutenberg editor using the following:
function dnl_theme_add_editor_styles() {
add_editor_style();
}
add_action( 'after_setup_theme', 'dnl_theme_add_editor_styles' );
This worked perfectly well (editor-style.css
loaded in Gutenberg) until I upgraded to WordPress 6.2. After that, the stylesheet stopped getting loaded in the editor. I tried loading style.css
using add_editor_style
like so:
function dnl_theme_add_editor_styles() {
add_editor_style( 'style.css' );
}
I also tried calling add_theme_support
first, e.g.:
function dnl_theme_add_editor_styles() {
add_theme_support( 'wp-block-styles' );
add_editor_style();
}
But none of that helped.
Has anyone else noticed this behavior after upgrading? Does anyone know why this would be happening? I looked at the source code for add_editor_style
in wp-includes/theme.php
and didn't see any changes there since 6.1.1.
2 Answers
Reset to default 1UDATE
Following @TomJNowell's comments, I tried removing container queries from my stylesheets, and as add_editor_style
worked as expected. This seems like a bug to me, though there may be a logical explanation for this I am not aware of. In my case, I think I would prefer to keep using container queries, so I will probably be sticking to my JS solution below, but I figured it was worth flagging.
Previous answer
In lieu of an actual fix to add_editor_style
, I have the following partial workaround/hack in place for now, in case it is helpful for others with this problem:
In functions.php:
function dnl_load_theme_styles_in_gutenberg() {
wp_enqueue_script(
'gutenberg-styles.js',
get_template_directory_uri() . '/gutenberg-styles.js',
);
}
add_action( 'enqueue_block_editor_assets', 'dnl_load_theme_styles_in_gutenberg' );
In gutenberg-styles.js
:
window.addEventListener('DOMContentLoaded', () => {
const themeStyleTag = document.createElement('link');
themeStyleTag.setAttribute('rel', 'stylesheet');
themeStyleTag.setAttribute('href', `/wp-content/themes/my-theme-name/style.css?v=${new Date().getTime()}`);
document.querySelector('body').appendChild(themeStyleTag);
});
Assuming the CSS you want to load into Gutenberg is called style.css
and is located in your theme root, and you swap out my-theme-name
with the name of your theme, this should work.
I should note one important difference between doing this and using add_editor_style
: the latter prefaces all your CSS file's selectors with .editor-styles-wrapper
, whereas the former does not. So, if your theme CSS includes, e.g.
.my-custom-block {
background-color: limegreen;
color: hotpink;
}
And you inject your theme CSS into Gutenberg with add_editor_style
, Gutenberg will inline the following CSS in the editor view:
.editor-styles-wrapper .my-custom-block {
background-color: limegreen;
color: hotpink;
}
...changing the specificity of the selector.
Whereas my JS-based hack will just add the same CSS block you had in your theme CSS, since all it does is load the theme CSS into the editor.
This behavior can happen also when there's syntax error in the CSS. On frontend the browser can ignore the error (like missing }
) but the editor cannot parse it.
本文标签: updatesaddeditorstyle not working after upgrade to WP v 62
版权声明:本文标题:updates - add_editor_style not working after upgrade to WP v 6.2 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736660231a1946377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_theme_support( 'editor-styles' )
? – Jacob Peattie Commented Apr 11, 2023 at 14:42add_action
call, as well as within the functiondnl_theme_add_editor_styles
called by theadd_action
call copied above, with and withoutadd_theme_support( 'wp-block-styles' )
). No combination seemed to have any effect; the theme styles still aren't loading in Gutenberg. – Eric Commented Apr 11, 2023 at 15:00