Docs: Add register_block_type_args
as a way to filter registration of a block in the documentation
#54112
Labels
[Feature] Block API
API that allows to express the block paradigm.
[Feature] Extensibility
The ability to extend blocks or the editing experience
[Type] Developer Documentation
Documentation for developers
When the dev need this docs ?
When creating a plugin that add new
attributes
to blocks that the plugin itself doesn't control (example are core blocks or blocks from other plugins), dev uses some filter hooks in PHP and React side to accomplish the goal.Why current situation is not optimal
Currently, this docs https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#registration
does include some hooks used to alter the regitration phase of a block.
But it lacks the PHP filter
register_block_type_args
that is mandatory to use for a plugin that add attributes to block.Additional Notes
While doing a plugin that add "attributes" to all blocks I faced some obstacles, that documentation could improve to help other with same requirements.
To add a new "attribute" to a block you need to add UI to the editor, used for controlling the new attribute.
This is done in the React side (with
editor.BlockEdit
filter hook) and wrapper component thatsetAttributes
.Doing that is ok from the "funcitonal" side (it works) but is not optimal and its fragile.
You can encounter "validation" error with some blocks, because while you are using the Editor, the editor itself transmit block data to PHP server to perform validation of attributes.
The validation DOESN'T ALLOW unknown attributes, so often validation fails and you have a "broken" blocks in the editor.
It is good to say that often the validation fails, the block is broken on the editor, but it is rendered correctly in the frontend.
Going back to topic, the validation uses the schema of the registration phase to do its job, so it is mandatory that you will add your "attributes" in the registration.
The registration phase of block can happens both in PHP or React side, so the dev needs to augment the block schema in both sides.
In JS, dev uses
blocks.registerBlockType
filter hook.In PHP, dev uses
block_type_metadata
filter hook.All these hooks are present in the documentation https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#registration
But i encounterd again the validation error with some thirdy party blocks, and after a debug session i found that , in PHP side, is better to replace
block_type_metadata
usage withregister_block_type_args
.The React side is ok.
The text was updated successfully, but these errors were encountered: