REST API driven Block Editor UI or how to remove the Author selector
How would you go about removing certain UI elements from the Block Editor? Suppose you want to remove the default Author selector to replace it with a component that supports multiple authors.
In the pre-Gutenberg era, or if you are still using the Classic Editor (or ClassicPress), you could remove the author selector by removing the meta box containing it:
remove_meta_box( 'authordiv', 'post', 'normal' );
After removing the default, you can register a new meta box with add_meta_box
.
This approach seems straightforward: you can add, remove, or alter UI elements with specific functions and filters without affecting other parts of the system.
How do you do the same in the Gutenberg era?
Gutenberg has something similar to "meta boxes", called Panels. Pretty much the entire Settings Sidebar consists of these.
But this information doesn't help much, as the PostAuthorPanel
is included in the PostSummary
in such a way that there’s no way to remove it with any functions or filters.
(It's slightly misleading that the PostAuthorPanel
is not a "panel", or at least it doesn't resamble one. Panels are defined as a components that "[...] expand and collapse multiple sections of content".)
Besides the PostAuthorPanel
component there's the PostAuthor
. This "renders the component for selecting the post author" and is wrapped in the PostAuthorCheck
.
Imagine the components nested within each other as shown here:
<PostSummary>
<PostAuthorPanel>
<PostAuthorCheck>
<PostAuthor />
</PostAuthorCheck>
</PostAuthorPanel>
</PostSummary>
This PostAuthorCheck
makes sure that it only "renders its children only if the post type supports the author".
This suggests that we may need to remove the author
support:
remove_post_type_support( 'post', 'author' );
However, this is quite drastic. Removing the support has various effects, from removing the author column from the post listing to disabling aspects in the REST API.
Not ideal.
How does the Block Editor determine whether a post type supports authors? This is truly the paramount question.
It has something to do with the post support, but not in the sense that the Block Editor directly checks whether the post supports the author.
It does it in a more indirect way.
It checks the existance of wp:action-assign-author
in the REST API's _links
.
More or less, it does it like so:
const hasAssignAuthorAction =
!!wp.data.select('core/editor').getCurrentPost()._links[
'wp:action-assign-author'
] ?? false;
This is a very different way of thinking. We have parts of the Block Editor UI that are driven by the REST API.
The solution to remove the author "panel" is to keep the post support (avoiding side effects) but modify the REST API response to remove the https://api.w.org/action-assign-author
link.
This is what, for example, the Co-Authors Plus does as well.
This is nothing new. It's been like this since 2018. And if anyone wants to see what spawned this need and which options were initially discussed they refer to the issue #6361.