WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 5 days ago

#51258 new enhancement

Fire an action when `replace_editor` is filtered to true

Reported by: jeremyfelt Owned by:
Milestone: 5.7 Priority: normal
Severity: normal Version: 4.9
Component: Editor Keywords: has-patch needs-refresh
Focuses: administration Cc:

Description

The replace_editor filter was added during the development of Gutenberg to make it possible to implement multiple editor options. See [41829].

Gutenberg previously used this filter to inject necessary hooks and markup before returning true to indicate that the editor had been replaced.

The replace_editor filter is no longer used by the block editor or the Gutenberg plugin—as it has become the editor. Instead, we rely on use_block_editor_for_post() to determine whether the block or classic interface should load.

In the current state, if true is returned via replace_editor, the current screen is set and the admin footer markup is loaded. There is no clean opportunity to add markup or other logic. This can be done during the filter, but that does not provide a clean pattern, especially if one or more pieces of code may be making a decision on editor replacement. See #50216 for a description of how it may fire multiple times.

The patch attached to this ticket adds a replaced_editor action in the post-new.php and post.php files so that plugins can cleanly inject replacement editor code.

Attachments (1)

51258.diff (1.0 KB) - added by jeremyfelt 5 months ago.

Download all attachments as: .zip

Change History (8)

@jeremyfelt
5 months ago

#1 in reply to: ↑ description @teamdnk
4 months ago

There is no clean opportunity to add markup or other logic. This can be done during the filter, but that does not provide a clean pattern, especially if one or more pieces of code may be making a decision on editor replacement.

I agree. Currently, this is a problem for us as we are wanting to use a standalone block editor tailored for a specific post type. At the moment we are using custom actions to implement our own standalone editor, but this requires us to re-route all edit related functions and related logic to our new action such as get_edit_post_link().

@jeremyfelt Thanks for the report and patch submission.

#2 follow-up: @noisysocks
8 weeks ago

  • Keywords reporter-feedback added

Hi @jeremyfelt. Do the enqueue_block_editor_assets or enqueue_block_assets actions not suit your use case?

#3 in reply to: ↑ 2 ; follow-up: @jeremyfelt
8 weeks ago

  • Keywords reporter-feedback removed

Replying to noisysocks:

Hi @jeremyfelt. Do the enqueue_block_editor_assets or enqueue_block_assets actions not suit your use case?

In this use case, replace_editor is filtered to true, so the block editor is not loaded and neither of those actions are available.

A replaced_editor action would be used to render markup and scripts for a completely different editor.

#4 @noisysocks
8 weeks ago

  • Milestone changed from Awaiting Review to 5.7

Aha, got it, thanks :)

#5 in reply to: ↑ 3 @azaozz
8 weeks ago

Replying to jeremyfelt:

This filter wasn't named that well, perhaps should have been something like replace_editor_template as that's what it does.

A replaced_editor action would be used to render markup and scripts for a completely different editor.

Right, an action would solve the problem with the "missing" edit-form-advanced.php when replace_editor returns true. There are some caveats though. As mentioned admin-footer.php is still included, but admin-header.php is not. Also things like $current_screen->is_block_editor( false );, missing PHP globals like $post_ID, etc. Perhaps adding a big, nice docblock would help somewhat.

Last edited 8 weeks ago by azaozz (previous) (diff)

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


7 days ago

#7 @hellofromTonya
5 days ago

  • Keywords needs-refresh added

Adding needs-refresh for Ozz's comments.

Note: See TracTickets for help on using tickets.