Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#53026 new defect (bug)

register_block_type_from_metadata fails if path points to a JSON file that is not named "block.json"

Reported by: artifacts's profile artifacts Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5
Component: Editor Keywords: 2nd-opinion
Focuses: Cc:


If a path to a file is given that does not end with "block.json", the function will exit early and the block won't be registered because the path to the json file is not found.

The documentation states that either a directory or a path to a JSON file can be given:

Parameters #Parameters
(string) (Required) Path to the JSON file with metadata definition for the block or path to the folder where the block.json file is located.


register_block_type_from_metadata() is called with $path = path_join(DIR,'block-section.json'):

$path = path_join(__DIR__,'block-section.json');

in register_block_type_from_metadata this test is performed:


function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
195             $filename      = 'block.json';
196             $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
197                     trailingslashit( $file_or_folder ) . $filename :
198                     $file_or_folder;
199             if ( ! file_exists( $metadata_file ) ) {
200                     return false;
201             }

wich results in


"ction.json" is not equal to $filename ("block.json"), so the $metadata_file will be assigned to a wrong path, in this case


Change History (1)

#1 @desrosj
3 years ago

  • Component changed from General to Editor
  • Keywords 2nd-opinion added
  • Version changed from trunk to 5.5

Welcome to Trac, @artifacts!

Thanks for this report. You are correct and I am able to confirm this behavior. Looks like [48141] introduced the issue in 5.5.

Before a fix can be created, I'd like to get some feedback from some editor contributors that would know more about the background of this.

Specifically, I'd like clarity on whether it is preferred that block files always be named block.json and this was meant to enforce this as a best practice (the docs should be updated to be more clear in this case), or if this is an oversight and any name is permitted for the JSON file (the code needs to be corrected).

Note: See TracTickets for help on using tickets.