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
$path
stringrequired- The file or folder path to determine the block identifier from.
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. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.