Skip to content
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

Generate body class for Gutenberg pages & provide helper function #4418

Closed
maddisondesigns opened this issue Jan 12, 2018 · 11 comments
Closed
Labels
Needs Decision Needs a decision to be actionable or relevant Needs Dev Ready for, and needs developer efforts [Type] Enhancement A suggestion for improvement. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.

Comments

@maddisondesigns
Copy link

Issue Overview

I think that it's pretty obvious by now that there's a large portion of the WP community that are still going to need to continue to use the classic editor once WP5.0 ships. Not only that, when providing themes in the .org Theme Directory, it's going to be important that the theme supports both the classic editor as well as the new Gutenberg markup.

I know it's been mentioned (briefly) in the past on a ticket somewhere, but that discussion never went anywhere. I think it's important that WP adds a new gutenberg body class to any pages that are rendered by Gutenberg. The styles required to render content from the classic editor and also from Gutenberg, are going to be significantly different and we need a way to know what editor is rendering the page.

Likewise, it would also be good to have a helper function so that our PHP can know what editor is being used for the current page. Something like is_gutenberg(). This will be helpful if people want to enqueue different stylesheets or scripts, depending on what editor is being used.

I think it's important that these are supported core functions as well, rather than just adhoc code snippets that we have to manually add to each of our themes to get this sort of functionality.

@jeffpaul jeffpaul added [Type] Enhancement A suggestion for improvement. Backwards Compatibility Issues or PRs that impact backwards compatability labels Jan 26, 2018
@karmatosed karmatosed removed the [Type] Enhancement A suggestion for improvement. label Apr 27, 2018
@pm-hiroshi
Copy link

Hi to @maddisondesigns .
I was looking for is_gutenberg() too.
Then I found existing gutenberg_post_has_blocks() which works as is_gutenberg() so far.
Actually this code did the job. ( $post can be either post object or post_id. )

function is_gutenberg( $post ) {
	return gutenberg_post_has_blocks( $post );
}

I am sorry if you knew gutenberg_post_has_blocks() exists.

PS
This is my very first comment to WordPress repositories.
And sorry for my bad English.

@maddisondesigns
Copy link
Author

@pm-hiroshi Thanks for that. I wasn't aware that gutenberg_post_has_blocks() existed, so I appreciate you letting me know. That'll be very useful

@pm-hiroshi
Copy link

pm-hiroshi commented Jun 9, 2018

We used to think that is_some_kind_of_condition() always exists.
So I still wish that is_gutenberg() exists.

@mtias
Copy link
Member

mtias commented Jul 9, 2018

I think a has-blocks class or similar would be better as part of post_class in index pages and body class for singular.

@mtias mtias added [Type] Enhancement A suggestion for improvement. and removed Backwards Compatibility Issues or PRs that impact backwards compatability labels Jul 9, 2018
@maddisondesigns
Copy link
Author

@mtias That would be fine. Thanks. It obviously makes sense to get away from the name 'Gutenberg' so as long as there's something that we can use, both in php and css, I don't really mind what it's called.

@designsimply designsimply added [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. Needs Dev Ready for, and needs developer efforts labels Jul 25, 2018
@mtias mtias mentioned this issue Aug 1, 2018
16 tasks
@obenland
Copy link
Member

obenland commented Aug 3, 2018

Can you help me better understand what that body class is for?

The styles required to render content from the classic editor and also from Gutenberg, are going to be significantly different

Do you mean the HTML/CSS class structure here or displaying a gallery differently?

@maddisondesigns
Copy link
Author

@obenland Yep, the html/css. The html/css that is output on the front-end is significantly different when using Gutenberg, compared to using the Classic Editor. As someone who has themes on .org, we're now going to need to support both editors in our theme so it would be great if Gutenberg added a body class so as to help us style for the different editors.

Likewise, it would also be good to have a helper function so that our PHP can know what editor is being used for the current page. Something like is_gutenberg() or is_block_editor() . This will be helpful if people want to enqueue different stylesheets or scripts, depending on what editor is being used.

@obenland
Copy link
Member

obenland commented Aug 3, 2018

it would be great if Gutenberg added a body class so as to help us style for the different editors
Wouldn't the differently structured html/css itself giving it away? CSS classes starting with wp-blocks-* are also a good giveaway. If the only difference between two versions is the body class, the body class would be obsolete.

I'm just having reservations when it comes to adding a Core body class because that class will have to be supported for the rest of time. And with a future so close where every post will be made out of blocks, having a has-blocks class floating in there doesn't feel very useful.
A theme can do whatever it wants, including adding any body class it needs, I'm just not sure it should be Core-provided.

This will be helpful if people want to enqueue different stylesheets or scripts, depending on what editor is being used

Gutenberg currently comes with the enqueue_block_editor_assets action for that. In case that doesn't get ported over, additional scripts/styles could be enqueued based on what wp_script_is( 'wp-edit-post' ) and such returns.

@maddisondesigns
Copy link
Author

And with a future so close where every post will be made out of blocks, having a has-blocks class floating in there doesn't feel very useful.

There are going to be tens of thousands of sites that aren't using blocks and will be using the Classic Editor. For our Themes on .org. it's also imperative that they continue to support the Classic Editor whilst adding new features for Gutenberg.

With that said, this issue was raised almost 7 months ago and obviously a lot has changed since then. I've been able to get around the lack of a body class so far, so it's probably not quite as important anymore.

Gutenberg currently comes with the enqueue_block_editor_assets action for that.

As far as I'm aware, enqueue_block_editor_assets is just for loading assets in the Dashboard/Editor. I was hoping to get something like is_gutenberg() or is_block_editor() for the front-end. i.e. So we can enqueue different assets depending on whether the page is being rendered by Gutenberg or by the Classic Editor

obenland added a commit that referenced this issue Aug 6, 2018
Merges `gutenberg_post_has_blocks()` and `gutenberg_content_has_blocks()`, resulting in a template function that can be used context-independently.
It accepts a content string, a post object, or post ID, and default to the current post if none of those are given. Make this part of the API behave close to existing WordPress template functions.

It's my first Gutenberg PR, so I'd love to get feedback on this, especially around a better phpunit setup potentially?

See #4418.
pento pushed a commit that referenced this issue Aug 16, 2018
This deprecates `gutenberg_post_has_blocks()`.

See #4418, #8352.
peiche added a commit to peiche/aesop-core that referenced this issue Aug 17, 2018
The function `gutenberg_content_has_blocks` is deprecated as of version 3.6.0. The discussion leading up to the change can be found [here](WordPress/gutenberg#4418) and the PR which was merged 2 days ago can be found [here](WordPress/gutenberg#8631).
@webmandesign
Copy link
Contributor

I was actually implementing similar body class in my theme a week ago. Then it occurred to me and I'm with @obenland in opinion on such class.

We shouldn't actually apply any class just because the Gutenberg is used on the post/page. Gutenberg will become standard and what will we do with the class then...? ;)

If anybody really needs such a class, just implement the functionality into your theme. But for making your theme future proof, I'd suggest using negative class, something like:

function themeprefix_body_class_blocks( $classes ) {
	if ( is_singular() && ! has_blocks() ) {
		$classes[] = 'has-no-blocks';
	}
	return $classes;
}
add_filter( 'body_class', 'themeprefix_body_class_blocks' );

Then you can apply specific styles to Gutenberg pages using body:not(.has-no-blocks) CSS selector.

But than again, once you update your theme for Gutenberg in the future, you will most likely rewrite the CSS, so it's really up to you if you use has-blocks or has-no-blocks class B-)

@mtias mtias removed this from the Merge Proposal: Themes milestone Oct 3, 2018
@danielbachhuber danielbachhuber added the Needs Decision Needs a decision to be actionable or relevant label Oct 30, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0 milestone Oct 30, 2018
@mtias
Copy link
Member

mtias commented Nov 1, 2018

Leaving this is in user land seems good for now. It can always be added later if the need becomes more clear.

@mtias mtias closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Decision Needs a decision to be actionable or relevant Needs Dev Ready for, and needs developer efforts [Type] Enhancement A suggestion for improvement. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.
Projects
None yet
Development

No branches or pull requests

9 participants