Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#49615 new enhancement

Editor: Support filtering arguments in block type registration

Reported by: aduth Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Editor Keywords:
Focuses: Cc:


Related: https://github.com/WordPress/gutenberg/pull/18414

It is currently not possible to filter the settings of a block type during its registration. I would propose that this support be added for on the following basis.

  • To enable extensibility. See Gutenberg#18414 for one such example, where default block attributes are added automatically on the basis of block "supports".
  • For consistency with other register_ functions.

See prior art:

The attached patch seeks to follow this pattern:

  • Named register_block_type_args for consistency with prior art
  • Implemented in WP_Block_Type::set_props, after wp_parse_args, for consistency with `WP_Post_Type::set_props`

Attachments (1)

49615-filter-register-block-type.diff (1.8 KB) - added by aduth 4 weeks ago.

Download all attachments as: .zip

Change History (5)

#1 @gziolo
4 weeks ago

Nice work, I second this effort. It nicely aligns with the existing filters. Code looks good, I'd appreciate some sanity check though because I don't have a lot of experience with adding new hooks in core.

#2 @azaozz
4 weeks ago

Code looks good here too.

My only concern is what would happen (if anything) when registered block properties are removed.

Use case:

  • Plugin adds three more props to the Paragraph block.
  • The block is used in posts, including the three props.
  • Plugin is deactivated.
  • User edits one of the older posts that includes paragraph blocks with the three (now non-registered) props.

Similarly, this filter makes it possible to remove properties from the default/core blocks. Is that supported / is it a good idea?

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

#3 @aduth
4 weeks ago

@azaozz It's a good point to raise. For what it's worth, this sort of filtering exists already client-side:


The same concerns exist (and are valid) there as well. We tend to warn about manipulating block attributes by a plugin for the exact reasons you mention: That if the plugin becomes disabled, the block may be inadvertently put into an "invalidated" state. It's perhaps worth noting that it's not necessarily guaranteed that this would occur, and there may be cases where a plugin author or site owner might even be okay with that caveat.

The question is relevant for the use-case described in the initial comment, since this sort of filtering is necessary to be able to implement default block supports in Gutenberg. But if it's the sort of thing where we are only comfortable in implementing these because we assume they would become part of some core set of supported block supports, then it raises a question about whether the code as it would expect to be implemented in core would be achieved this way (via filter), vs. by some other means (directly in the logic of register_block_type or in class WP_Block_Type_Registry, for example).

Finally, block attributes aren't the only thing one might possibly want to filter. It could be any of these:


Plus the server-side render_callback, editor_script, script, editor_style, and style.

How might that be used? Not entirely sure, but maybe:

  • Extending render_callback to add markup in addition to the default implementation (technically possible now by other means, i.e. render_block filter)
  • Changing category or keywords

So, I'm on the fence 🙂

One one hand, there's quite a bit of prior art with this sort of filtering (register_post_type_args, etc), it would bring consistency with the client-side implementation (blocks.registerBlockType filter), and it's partly granted in the intent with filtering that we won't necessarily care to know the full extent of how we'd expect the filter to be used.

On the other hand, it could be a "footgun". And at the very least, this thought exploration has me considering whether we really care to be implementing block supports in Gutenberg first, vs. just exploring the patch in core. And if so, if we might not even need this filter for that immediate use-case.

#4 @gziolo
4 weeks ago

The nice side-effect of using register_post_type_args is the use case where you could set render_callback using this filter and automate the block registration using some sort of block.json detection from one central place. If you combine it with the new utility function register_block_type_from_metadata I propose in https://github.com/WordPress/gutenberg/pull/20794, you could simplify the block registration to running a loop over the list of locations with block.json file or even provide another filter that stores the list of such locations. This way, we could avoid the whole logic in Gutenberg that plays with action priorities to ensure that core blocks are successfully overridden in the plugin:

Instead, it could be simplified to two things:

  • Gutenberg replaces the list of locations for core blocks
  • Gutenberg registers filters that override render_callback for core blocks (well, here the priority might be still an issue as it's the same code)
Last edited 4 weeks ago by gziolo (previous) (diff)
Note: See TracTickets for help on using tickets.