-
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
Block Editor: Add filter to enable custom classes in "editor-styles-wrapper" #17854
Comments
This would be nice - and it would require JavaScript to change the body class interactively. @CreativeDive: |
@CreativeDive: There is also https://github.com/generoi/wp-gutenberg-templates, which can be helpful. |
This issue has surfaced countless times over the years, and I hear this request a lot from theme developers, especially those transitioning from classic to hybrid themes. While there is currently no solution, I believe the comment here hints at a possible solution. |
We have run into LOTS of situations where more body classes in the editor would go a long way. Here are just a few examples:
Themes and plugins have been relying on body classes for years, and bringing those classes - or at least the ability to add them - to the editor feels essential before the page/post editor gets iFramed in 6.2. |
Yea, seems that would be the way to do it — instead of selectively adding to the editor. |
The post editor already adds already added @MadtownLems I'm curious how much you're styling per template outside of Global Styles? What are the use cases for not using Global Styles/Site Editor for applying styles? And are you using classic themes — or block themes? |
It currently adds them to the body class, yes. However, starting in 6.2, the page/post is going to be inside an iFrame. As a result, they will no longer be accessible to the blocks. (And the body classes are already not accessible in the Site Editor, as that's already inside an iFrame)
We are using classic themes with FSE-style features such as Template Parts. |
@richtabor I believe this is largely for classic and hybrid themes that are using the Block Editor and are attempting to make the Editor look more like the frontend. Jason can confirm this. That said, I do see other applications for this functionality for third-party extenders. |
@ndiego No, I need this, and I do not use a classic or hybrid theme. WordPress/GB adds classes to the body based on what template is used. I use those classes to style my theme. Very important styles like Why would this "largely for classic and hybrid themes"? WP adds all kinds of classes to the body on the frontend, and they are as relevant in Block Themes as they always were. |
Yup, there are tons of applications for the functionality. The bulk of the requests I have seen are from classic and hybrid theme developers, but that doesn't mean that it's also not useful for block theme developers as well. |
I was modifying a block editor layout on a page edit screen using custom admin body class and then targeting the However, since some current version of Gutenberg (I am using v16.2.1 currently) the page block editor is enclosed within So, it seems this issue is more relevant than ever: |
Put this on my to do list. Not sure if we should do this in JS or make a request to the server. 🤔 |
My initial reaction is ideally both. For example, consider a custom class (or classes) that reflect a custom page template.
Another time I use custom body classes (and what editor classes) is when checking the editor's content for a certain condition like a Post Title block as the first block. Again, I want that available on page load if it's present in the saved content and immediately added if the block is inserted/moved in that position. |
I definitely suggest server side (PHP) as it is useful when using plugins. For example, I was modifying block min-width in editor when user was editing WooCommerce "Shop" page content. In such case I can use PHP functions to check whether such page is being edited. I personally have no usecase for JS class manipulation currently, so I can't really provide feedback for that. |
Yes, PHP would go a long way. I've relied on the admin_body_class filter in all my projects. As @CreativeDive mentioned, a |
Any ETA on this? 😅 I tried to update editor body classes from outside ( EDIT : Found a fix that seems to work for now (atleast for my usecase), load this script using ((wp) => {
const { domReady } = wp;
/**
* Update iframe classes
* @see https://github.com/WordPress/gutenberg/issues/17854
* @see https://github.com/WordPress/gutenberg/issues/56831
* @see https://github.com/WordPress/gutenberg/issues/55947#issuecomment-1801105188
*/
domReady(() => {
const editorBodyEl = document.querySelector('.editor-styles-wrapper');
// Need to check context because this script will be loaded twice (once in the admin page, once in the editor)
// @see https://github.com/WordPress/gutenberg/issues/53590
const isIframeEditorContext = !!editorBodyEl;
if (isIframeEditorContext) {
setTimeout(addBodyClassesToIframe, 300); // setTimeout is needed otherwise classes get erased if added too early
}
// This add all body classes on the editor iframe (adapt to your needs)
function addBodyClassesToIframe() {
const adminBodyEl =
window.parent.document.getElementsByTagName('body');
const adminClasses = adminBodyEl?.[ 0 ]?.classList;
if (editorBodyEl && adminClasses) {
adminClasses.forEach((adminClass) => {
editorBodyEl.classList.add(adminClass);
});
}
}
});
})(window.wp); |
I just tested that idea out (using the results of get_body_class), but it doesn't do much more than printing out More so, would we want it to add contextual or template/page-specific classes to the iframed body tag? I mean classnames that are rendered on the frontend, e.g., |
Any updates on this? |
I've hit this issue but in relation to block templates and the new Section Style feature https://make.wordpress.org/core/2024/06/24/section-styles/ I made a section style for a set of post types, but lets say we simplify that down to just the <!-- wp:template-part {"slug":"header"} /-->
<!-- wp:group {"className":"is-style-article","layout":{"type":"constrained"}} -->
<div class="wp-block-group is-style-article">
<!-- wp:post-content /-->
</div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer"} /-->
That section style is used to set a bunch of different defaults on the blocks inside the article like margins, typography, spacing etc... This works as you would expect on the front end, but of course in the admin when editing a post the containing group block is missing. So one solution could be to set the post type template to include the outer group that has the section styling. That's probably what I'll go for in this instance, and it could be the documented approach in lieu of the new functionality around template coming in WP 6.7 https://developer.wordpress.org/news/2024/08/29/registering-block-templates-via-plugins-in-wordpress-6-7/ |
Actually looks like section styles don't preview/apply in the editor so maybe not really ready yet. |
Why should it be possible to add custom classes to the block editor "editor-styles-wrapper" container?
For themes the
add_filter( 'body_class', 'my_body_classes' );
is extremely useful to style templates in certain cases, such as:So we can set certain styles, such as:
If we using
add_editor_style()
, currently this has no effect on the block editor, because the specific body classes are not a part of the "editor-styles-wrapper" container.How we can handle this? A new filter for theme developers would be great, such as:
Or is there an other way?
The text was updated successfully, but these errors were encountered: