register_block_type_from_metadata() – Registers a block type from the metadata stored in the `block.json` file.
You appear to be a bot. Output may be restricted
Description
Registers a block type from the metadata stored in the block.json
file.
Usage
$WP_Block_Type|false = register_block_type_from_metadata( $file_or_folder, $args );
Parameters
- $file_or_folder
- ( 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. If providing the path to a JSON file, the filename must end with `block.json`. - $args
- ( array ) optional – Optional. Array of block type arguments. Accepts any public property of `WP_Block_Type`. See WP_Block_Type::__construct() for information on accepted arguments. Default empty array.
Returns
WP_Block_Type|false The registered block type on success, or false on failure.
Source
File name: wordpress/wp-includes/blocks.php
Lines:
1 to 100 of 122
function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $filename = 'block.json'; $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ? trailingslashit( $file_or_folder ) . $filename : $file_or_folder; if ( ! file_exists( $metadata_file ) ) { return false; } $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { return false; } $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); /** * Filters the metadata provided for registering a block type. * * @since 5.7.0 * * @param array $metadata Metadata for registering a block type. */ $metadata = apply_filters( 'block_type_metadata', $metadata ); // Add `style` and `editor_style` for core blocks if missing. if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); if ( ! isset( $metadata['style'] ) ) { $metadata['style'] = "wp-block-$block_name"; } if ( ! isset( $metadata['editorStyle'] ) ) { $metadata['editorStyle'] = "wp-block-{$block_name}-editor"; } } $settings = array(); $property_mappings = array( 'apiVersion' => 'api_version', 'title' => 'title', 'category' => 'category', 'parent' => 'parent', 'icon' => 'icon', 'description' => 'description', 'keywords' => 'keywords', 'attributes' => 'attributes', 'providesContext' => 'provides_context', 'usesContext' => 'uses_context', 'supports' => 'supports', 'styles' => 'styles', 'variations' => 'variations', 'example' => 'example', ); $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; $i18n_schema = get_block_metadata_i18n_schema(); foreach ( $property_mappings as $key => $mapped_key ) { if ( isset( $metadata[ $key ] ) ) { $settings[ $mapped_key ] = $metadata[ $key ]; if ( $textdomain && isset( $i18n_schema->$key ) ) { $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); } } } if ( ! empty( $metadata['editorScript'] ) ) { $settings['editor_script'] = register_block_script_handle( $metadata, 'editorScript' ); } if ( ! empty( $metadata['script'] ) ) { $settings['script'] = register_block_script_handle( $metadata, 'script' ); } if ( ! empty( $metadata['viewScript'] ) ) { $settings['view_script'] = register_block_script_handle( $metadata, 'viewScript' ); } if ( ! empty( $metadata['editorStyle'] ) ) { $settings['editor_style'] = register_block_style_handle( $metadata, 'editorStyle' ); } if ( ! empty( $metadata['style'] ) ) { $settings['style'] = register_block_style_handle( $metadata, 'style' ); }