Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



register_block_type_from_metadata ›

Seit5.5.0
Veraltetn/v
register_block_type_from_metadata ( $file_or_folder, $args = array() )
Parameter: (2)
  • (string) $file_or_folder Path to the JSON file with metadata definition for the block or path to the folder where the `block.json` file is located.
    Erforderlich: Ja
  • (array) $args { Optional. Array of block type arguments. Any arguments may be defined, however the ones described below are supported by default. Default empty array. @type callable $render_callback Callback used to render blocks of this block type. }
    Erforderlich: Nein
    Standard: array()
Gibt zurück:
  • (WP_Block_Type|bool) The registered block type on success, or false on failure.
Definiert in:
Codex:

Registers a block type from metadata stored in the `block.json` file.



Quellcode

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 = json_decode( file_get_contents( $metadata_file ), true );
	if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
		return false;
	}
	$metadata['file'] = $metadata_file;

	$settings          = array();
	$property_mappings = array(
		'title'           => 'title',
		'category'        => 'category',
		'parent'          => 'parent',
		'icon'            => 'icon',
		'description'     => 'description',
		'keywords'        => 'keywords',
		'attributes'      => 'attributes',
		'providesContext' => 'provides_context',
		'usesContext'     => 'uses_context',
		'supports'        => 'supports',
		'styles'          => 'styles',
		'example'         => 'example',
	);

	foreach ( $property_mappings as $key => $mapped_key ) {
		if ( isset( $metadata[ $key ] ) ) {
			$settings[ $mapped_key ] = $metadata[ $key ];
		}
	}

	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['editorStyle'] ) ) {
		$settings['editor_style'] = register_block_style_handle(
			$metadata,
			'editorStyle'
		);
	}

	if ( ! empty( $metadata['style'] ) ) {
		$settings['style'] = register_block_style_handle(
			$metadata,
			'style'
		);
	}

	return register_block_type(
		$metadata['name'],
		array_merge(
			$settings,
			$args
		)
	);
}