WP_Block_Metadata_Registry::default_identifier_callback()private staticWP 6.7.0

Default identifier function to determine the block identifier from a given path.

This function extracts the block identifier from the path:

  • For 'block.json' files, it uses the parent directory name.
  • For directories, it uses the directory name itself.
  • For empty paths, it returns an empty string.

For example:

  • Path: '/wp-content/plugins/my-plugin/blocks/example/block.json' Identifier: 'example'
  • Path: '/wp-content/plugins/my-plugin/blocks/another-block' Identifier: 'another-block'

This default behavior matches the standard WordPress block structure.

Метод класса: WP_Block_Metadata_Registry{}

Хуков нет.

Возвращает

Строку. The block identifier, or an empty string if the path is empty.

Использование

$result = WP_Block_Metadata_Registry::default_identifier_callback( $path );
$path(строка) (обязательный)
The file or folder path to determine the block identifier from.

Список изменений

С версии 6.7.0 Введена.

Код WP_Block_Metadata_Registry::default_identifier_callback() WP 6.7.1

private static function default_identifier_callback( $path ) {
	// Ensure $path is not empty to prevent unexpected behavior.
	if ( empty( $path ) ) {
		return '';
	}

	if ( str_ends_with( $path, 'block.json' ) ) {
		// Return the parent directory name if it's a block.json file.
		return basename( dirname( $path ) );
	}

	// Otherwise, assume it's a directory and return its name.
	return basename( $path );
}