-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning when enqueuing styles for a plugin in iframed Full Site Editor #33212
Comments
I was literally just about to report this, thanks @leeshadle. We need a safe/official way to enqueue styles that are not necessarily "part" or a block or theme. This use-case will only increase as more and more developers build for the Full Site Editor. |
Ideally we should be using |
Presumably |
@ndiego Here is discussion about this issue: https://make.wordpress.org/core/2021/06/29/blocks-in-an-iframed-template-editor/#comment-41512 |
I tried using With that being said, using
This is the case for me as well. I'm also using inline styles to print my own CSS variables and it doesn't work in the iframed editor. I created an issue for this here if you want to follow along: |
It would if we add support for passing WP dependency handles in addition to passing a URL.
Why? :)
You should normally never have to worry about that.
You can, through
|
Thanks for the clarifications @ellatrix. Do you envision |
Thank you for talking through all of this @ellatrix! Again, all of this only becomes an issue when the block editor is iframed. Otherwise, everything works as expected.
Using
Further, I've tried using function theme_support () {
// trying to enqueue style.css by passing the correct directory to add_editor_style using plugin_dir_url()
add_theme_support( 'editor-styles' );
add_editor_style( plugin_dir_url( __FILE__ ). 'build/style.css' );
// trying to enqueue style.css by passing correct directory to add_editor_style using plugin_dir_path()
add_theme_support( 'editor-styles' );
add_editor_style( plugin_dir_path( __FILE__ ) . 'build/style.css' );
}
add_action( 'after_setup_theme', 'theme_support' ); Ultimately though, if all block editor instances are going to be iframed, is there any possible way we can continue to use |
No, I will make work of fixing |
Ahh that is very helpful info, thanks. I definitely missed that. 95% of my CSS is for the block editor UI and the remaining 5% touches the content of the editor. That must be why I am getting the error that was originally reported by Lee. I will strip that 5% out and try adding it separately with |
The error is there because there's a compatibility layer loading the stylesheets in the iframe if it detects "wp-block" or "editor-styles-wrapper" in a selector in the stylesheet. So the styles will load in the iframe, but they're not added as they should be. I'll add an integration test for |
Ahhh interesting... so it sounds like I should have been using
If you remove the rewriting in the iframe, would it enqueue the stylesheet, or would it inline the styles as it currently does? I ask b/c I often enqueue styles for scripts that add functionality to editor content - imagine something like adding flickity slider or masonry functionality in the editor where you need to enqueue scripts and styles. It'd be great if those styles can be cached and not inlined.
Does this mean this might look something like, we would use In the meantime, how would I go about using function theme_support () {
// trying to enqueue style.css by passing the correct directory to add_editor_style using plugin_dir_url()
add_theme_support( 'editor-styles' );
add_editor_style( plugin_dir_url( __FILE__ ). 'build/style.css' );
// trying to enqueue style.css by passing correct directory to add_editor_style using plugin_dir_path()
add_theme_support( 'editor-styles' );
add_editor_style( plugin_dir_path( __FILE__ ) . 'build/style.css' );
}
add_action( 'after_setup_theme', 'theme_support' ); |
That's what I mean, yes. Currently styles are inlined, but it doesn't have to be like that in the future. The styles had to be rewritten because there was no iframe. Ideally the API would look something like: add_action(
'init',
function() {
wp_register_style( 'my-plugin-style', url, dependencies ); // Don't enqueue, only register.
wp_add_inline_style( 'my-plugin-style', css );
add_editor_style( 'my-plugin-style' ); // Loads everything for you in the iframe.
}
); This is currently not possible yet. Behind the scenes, add_action(
'block_editor_settings_all',
function( $settings ) {
$settings['styles'][] = array( 'css' => 'p { border: 1px solid red }' );
return $settings;
}
); |
Thank you for taking the time to clarify all of this @ellatrix!
Love this - doing something like this would be great - flexible, clean, easy to migrate to, non-hacky ;) |
No problem! Thanks for the feedback! |
May I ask what kind of styles you're looking to registering for blocks that you don't register without being a theme? I'm just curious and want to learn more about use cases. |
Hey @ellatrix for my case, my plugin provides new blocks that have their own looks. Then I have a separate css file that's only loaded in the frontend that serve as common styles across all our blocks. Is that what you were asking about? |
I have a plugin called Block Visibility that allows users to conditionaly display blocks on the frontend of their website. When a block is conditional, I am adding a dashed outline around the block and adding some opacity. This greatly helps users know what is conditional in the editor when working with a large page full of blocks. Admittedly the implementation needs some fine tuning and there are a few issues with certain blocks. I know I am not the only one doing this and I envision other untility plugins adding styles to blocks in the editor. I am guessing Yoast SEO might be doing this with their Readability Analysis, but I am not 100% sure. The Readability Analysis highlights block content in purple, which is obviously not displayed on the frontend. |
@ellatrix I'm adding inline styles for the editor for my custom colors that user can choose in the Customizer.
Then I'm registering my custom CSS for the dashboard and adding inline styles for it on the 'admin_enqueue_scripts' hook and here is a result: And now I can use these color values for any of the editor elements: I really do not want to promote my theme here but you can check my code in it: adding colors in the /inc/setup.php on line 152 Thanks for your attention to this really important issue. |
Generally, I try to avoid creating custom blocks and use block filters with core blocks whenever possible. This typically requires enqueuing styles that apply to the editor content. Some examples of how I've done this:
|
@leeshadle did you ever get this working? |
@ndiego no, this isn't currently possible. For now, @ellatrix suggested extending the block editor settings instead. I haven't tried doing this, it doesn't really make sense for my use case as I've got a lot of styles being compiled into a stylesheet... |
Is there any update on this? After upgrading to 11.4, I am now seeing this in the Block Editor as well as the Site Editor. |
I'm running into this as well, within the Site Editor as well as with patterns loaded within the iFramed BlockPreview component (that's how I first noticed it). Seems like a bigger deal for third-party tools that extend core functionality that should be prioritized before 5.9 imo. |
@richtabor @ndiego @exstheme there is some chatter around this issue: |
Just a note, these errors are now warnings (see PR in #35914) |
Hi there! A PR that provides a solution to avoid these warnings, #37466, is ready for review. We are close to Beta 4, so testing and feedback is more than welcome 🙏 |
What about a way to tie function add_inline_css() {
wp_add_inline_style( 'wp-edit-blocks', 'p{background: red;}' );
}
add_action( 'init', 'add_inline_css' ); I have confirmed that the inline CSS is output in the iframe. |
--vk_image-mask-waveはコアの画像ブロックに依存するのでwp-edit-blocksを追加 参考 : WordPress/gutenberg#33212 (comment)
Running Core 5.8.3 and plugin 12.4.1, I'm enqueuing block CSS with a theme I'm working on using However, this file isn't loaded in the new site editor, meaning that none of the blocks there have any of my own CSS applied. If I enqueue it using What is the correct way to enqueue block CSS from one file in both the post/page and site editors? |
Update: the warning isn't thrown when using the latest 5.9 release candidate. |
@markhowellsmead notice the same here, however |
You were able to add styles from the plugin with add_editor_style()? |
@richtabor we use |
@richtabor I've attempted to enqueue a stylesheet from the theme I'm building. It doesn't work when using |
Addendum: |
@richtabor @markhowellsmead it looks like I'm able to enqueue styles in the iframed site editor ( without a warning ) via a plugin using |
I'm also having trouble to dynamically inject my color-variables to the iframes. enqueue_block_assets just doesn't do the trick. Is there any way to append/inject my styles to any of the other styles loaded inside the iframe? I can manage to load a css-file, but no inline-styles. I'm also seeing an empty <style>-tag after the last link-resource. |
I have the same issue as @maffi-git across all my sites. |
Is this issue still relevant after the 6.3 release. See https://make.wordpress.org/core/2023/07/18/miscellaneous-editor-changes-in-wordpress-6-3/#post-editor-iframed for more information about handling styles in the iframe. |
I believe this problem was solved in WordPress 6.3. For example, if some plugin wanted to apply a style to the editor content, including iframe editor instances, it could be accomplished with code like this: function my_stylesheet() {
if ( is_admin() ) {
wp_enqueue_style( 'my-style', plugins_url( 'my-style.css', __FILE__ ) );
wp_add_inline_style( 'my-style', '.editor-styles-wrapper p { color: red;}' );
}
}
add_action( 'enqueue_block_assets', 'my_stylesheet' ); |
@t-hamano My understanding is also that this is resolved with WP6.3 and that approach. Accordingly I'll close this issue. |
Description
There is a warning in the iframed Full Site Editor when enqueueing a stylesheet that isn't associated with a block.
I build a lot of plugins that use block filters only to enhance core blocks, which don't require creating custom blocks.
For plugins, the warning suggests using the block API's style or editorStyle:
I'm creating this issue b/c I'm concerned that there may not be a way to properly enqueue stylesheets in the iframed editor without being associated with a block.
Step-by-step reproduction instructions
WordPress information
Device information
The text was updated successfully, but these errors were encountered: