Make WordPress Core

Opened 4 years ago

Closed 2 years ago

Last modified 21 months ago

#51819 closed defect (bug) (fixed)

use_block_editor_for_post() not avaialble by front end

Reported by: themiked's profile theMikeD Owned by: jorbin's profile jorbin
Milestone: 6.1 Priority: normal
Severity: normal Version:
Component: Editor Keywords: reporter-feedback add-to-field-guide
Focuses: Cc:

Description

I have a use case where I need to know if a post is using the block editor in front end code. The normal function for this check would be use_block_editor_for_post() but this is loaded for admin-only, as part of wp-admin/includes/post.php

If there is an alternative I'm all ears, but this would seem to be the right function to use, and the docs give no indication that this is admin-only.

Attachments (1)

51819.diff (7.6 KB) - added by ethitter 2 years ago.

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
4 years ago

  • Component changed from General to Editor

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


3 years ago

#3 @poena
3 years ago

  • Keywords reporter-feedback added

Hi
This issue was discussed during a triage session today.
Can you give more details about the use case?
It was not clear if you are displaying an editor on the front, or if you want to know if any post uses blocks.

To find out if a post contains block you can try https://developer.wordpress.org/reference/functions/has_blocks/ or
https://developer.wordpress.org/reference/functions/parse_blocks/

#4 @ethitter
2 years ago

I'm involved in a project that is converting sites to use the Gutenberg editor. In several cases, we need to check at init if a particular post type is Gutenberg-enabled so that we can disable classic metaboxes and other functionality that we've re-implemented in Gutenberg components, but that is also still in use on non-Gutenberg post types. It's not possible to wait until a later action because these checks often must interact with plugins that we ourselves do not maintain, and which perform their setup at the init action. Ideally, use_block_editor_for_post() and use_block_editor_for_post_type() would be available at that point, which would be possible by moving them to wp-includes. Neither function depends on other functions that are available only in wp-admin (other than use_block_editor_for_post() relying on use_block_editor_for_post_type() and an admin-referrer check that's gated by a query variable), so moving them to wp-includes seems both feasible and beneficial.

Last edited 2 years ago by ethitter (previous) (diff)

@ethitter
2 years ago

#5 @jorbin
2 years ago

  • Milestone changed from Awaiting Review to 6.1

The use case that @ethitter make sense to me. Milestoning so that I can remember to commit it.

#6 @jorbin
2 years ago

  • Owner set to jorbin
  • Resolution set to fixed
  • Status changed from new to closed

In 53559:

Editor: Universalize functions for checking block editor status.

use_block_editor_for_post_type and use_block_editor_for_post can be very useful in more contexts than wp-admin, especially when a site is in transition. For example, you may want to do things on init that are different.

Neither function depends on other functions that are available only in wp-admin (other than use_block_editor_for_post() relying on use_block_editor_for_post_type() and an admin-referrer check that's historically gated by a query variable and now also gated by is_admin), therefore moving them to wp-includes seems both feasible and beneficial

Props ethitter, jorbin.
Fixes #51819.

#7 @milana_cap
21 months ago

  • Keywords add-to-field-guide added
Note: See TracTickets for help on using tickets.