W3cubDocs

/WordPress

WP_Block_Metadata_Registry::default_identifier_callback( string $path ): string

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

Description

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.

Parameters

$pathstringrequired
The file or folder path to determine the block identifier from.

Return

string The block identifier, or an empty string if the path is empty.

Source

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 );
}

Changelog

Version Description
6.7.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_block_metadata_registry/default_identifier_callback