Opened 4 years ago
Last modified 3 years ago
#53149 new feature request
Plugins: Consider using `block.json` file as an entry point for blocks from Block Directory
Reported by: | gziolo | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Plugins | Keywords: | 2nd-opinion |
Focuses: | Cc: |
Description (last modified by )
The original discussion sparked by a comment from @jipmoors on GitHub in https://github.com/WordPress/gutenberg/pull/13693#issuecomment-477904539:
WordPress automatically discovers all the block.json files in the plugin/core blocks folder and registers the corresponding block types. These block types are made available through the block registry (https://developer.wordpress.org/reference/classes/wp_block_type_registry/) PHP class, and the blocks scripts and styles are added as dependencies to the wp-block-library script and style handles.
At the moment, it's mandatory to create the PHP file for the plugin that contains only a single block type registration code when developing for Block Directory. Those plugins rarely have PHP code, so we could simplify the flow for developers by making it possible to use block.json
to source all required metadata. If the current metadata schema doesn't contain some fields that can be declared only in the PHP comment (some are duplicated in readme.txt
, we can always include it in block.json
.
In the case when developers would like to use the render_callback
, they could use the concept proposed in #53148 - the renderTemplate
file reference in block.json
. This way, the process of validation of block types submitted to Block Directory would get further simplified.
Change History (4)
#3
@
4 years ago
As far as I know, a PHP file is required because Block Directory plugins are just a specific type of plugin and have to adhere to the same requirements around required files.
Yes, all correct. This is why I'm proposing. Let's discuss if we could make the system more flexible and allow to skip PHP file when block.json
is present.
#4
@
3 years ago
An initial step could be also listing the folders to loads block.json
in the plugin metadata. You would still need the PHP file, but we could simplify block registration for devs and make it so much simpler to detect blocks for the Plugin and Block Directory.
The idea would be similar to have Domain Path works in the plugin's header:
https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#domain-path
https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields
It doesn't remove that much code:
However, it improve the developer experience and we would have better understanding of what the plugin contains like I mentioned above.
@gziolo I think that the Meta Trac may be a better place for this initial discussion to be had (it may not be possible with how the directory is set up).
As far as I know, a PHP file is required because Block Directory plugins are just a specific type of plugin and have to adhere to the same requirements around required files. The main PHP file in the plugin is used for the plugin headers so that the plugin can be activated/deactivated.