-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
dx-enhancement: add custom CSS in block editor iframe and frontend #48437
Comments
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
Is there a reason why you cannot register a custom block style or use https://developer.wordpress.org/reference/functions/wp_enqueue_block_style/ ? Have you seen the documentation for how to include CSS and JS? We moved some of the documentation yesterday to make it easier to find: https://developer.wordpress.org/themes/basics/including-css-javascript/#including-css-for-block-styles |
Yes , I can not use "wp_enqueue_block_style" because my css is not bounded to any block, but is global to all blocks. Thanks for posting the documentation link here. |
Block themes adds editor stylesheets using the same function classic themes do: https://developer.wordpress.org/reference/functions/add_editor_style/ |
Please don't add the same issue twice @tresorama #48454. I'm closing this for now as it's answered in two places. If you find any further bugs, you may re-open it. |
Thanks @carolinan , this is exactly what i was looking for. This issue is close. |
Update (2023-02-27 - 27 Feb)
There is a better solution, and simpler, to do what this issue originally describes.
In frontend use
wp_enqueue_style
and add dependency of css generated fromtheme.json
, so your will have priority if has same css specificity.In block editor iframe, use
add_editor_style
.Your css stylesheet does not need special treatments, it works in both contexts(block editor/site editor and frontend).
In frontend, you just add your stylesheet after the one generated from
theme.json
, to be able to overrides without increasing css specificity.In the block editor (and site editor) "iframe" you use the function add_editor_style, does exactly what this guide does manually, for the iframe part.
The function
add_editor_style
injects a CSS file in the block editor "iframe" and adds.editor-styles-wrapper
scoped selector to every css definition, also in media query definitions.For example,
.text-2xl
become.editor-styles-wrapper .text-2xl
Thanks to carolinan that suggested this in this issue.
End of update.
Original Issue Content
This issue is a mix of documentation and awareness of a weird behavior not well documented.
While working in a block theme for first time, i wanted to add custom css to the editor and the frontend. So that when i fill Sidebar > Advanced > Additional CSS classes for a block the preview applies these css classes.
I need that my custom CSS is injected in every editor page.
My custom CSS is global, is not related to any particular block.
How to add CSS to block editor iframe and in frontend
In a block editor page,
http://my-site.local/wp-admin/post.php?post=32&action=edit
the page dom contains an
<iframe name="editor-canvas">
that is the live preview (orange zone of image )Because this is an
<iframe>
, it can ONLY uses assets loaded inside its innerhtml
document.Assets loaded outside of the <iframe> will be ignored.
What we need is to "enqueue" assets to the iframe.
Currently there is no hook (in php) for that.
But some assets are "passed" to the iframe via html attribute (in the <iframe> node).
There is not an array that we can "add_filter" to, but there is an automatic behavior that takes place.
Speaking about a CSS file, Wordpress scan every "normally" enqueued assets, and create a subset of those and later "pass" this list to the iframe.
Then the iframe trigger network requests for fetching these.
How can be a CSS assets eligible for being passed to iframe?
It needs to contains some arbitrary css selector in its content,
.wp-block
and.editor-styles-wrapper
So, to load a css you MUST include required css selectors in file content, and then:
Weird behavior
It's very weird that we need to include
wp-block
andeditor-styles-wrapper
in css file content.I figured this only because of a great explanation in this issue in this comment #38673 (comment) , many thanks to @NenadObradovic for discovring that.
Honestly it's not nice, it would be great if in future this requirement will be dropped out.
The text was updated successfully, but these errors were encountered: