WP_Block_Metadata_Registry::default_identifier_callback( string $path ): string

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

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

VersionDescription
6.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.