函数原型
generate_block_asset_handle( string $block_name, string $field_name, int $index ): string
函数描述
Generates the name for an asset based on the name of the block and the field name provided.
是否弃用
未弃用
函数参数
-
$block_name
string
Required - Name of the block.
-
$field_name
string
Required - Name of the metadata field.
-
$index
int
Optional - Index of the asset when multiple items passed.
Default 0.
函数返回值
string Generated asset name for the block’s field.
函数位置
File: wp-includes/blocks.php.
函数源码
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
if ( 0 === strpos( $block_name, 'core/' ) ) {
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
if ( 0 === strpos( $field_name, 'editor' ) ) {
$asset_handle .= '-editor';
}
if ( 0 === strpos( $field_name, 'view' ) ) {
$asset_handle .= '-view';
}
if ( $index > 0 ) {
$asset_handle .= '-' . ( $index + 1 );
}
return $asset_handle;
}
$field_mappings = array(
'editorScript' => 'editor-script',
'script' => 'script',
'viewScript' => 'view-script',
'editorStyle' => 'editor-style',
'style' => 'style',
);
$asset_handle = str_replace( '/', '-', $block_name ) .
'-' . $field_mappings[ $field_name ];
if ( $index > 0 ) {
$asset_handle .= '-' . ( $index + 1 );
}
return $asset_handle;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 6.1.0 | Added $index parameter. |
| 5.5.0 | Introduced. |

