WP_Block_Metadata_Registry::default_identifier_callback
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.
Method of the class: WP_Block_Metadata_Registry{}
No Hooks.
Returns
String. The block identifier, or an empty string if the path is empty.
Usage
$result = WP_Block_Metadata_Registry::default_identifier_callback( $path );
- $path(string) (required)
- The normalized file or folder path to determine the block identifier from.
Changelog
| Since 6.7.0 | Introduced. |
WP_Block_Metadata_Registry::default_identifier_callback() WP Block Metadata Registry::default identifier callback code WP 6.9.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 );
}