函数文档

generate_block_asset_handle()

💡 云策文档标注

概述

generate_block_asset_handle() 函数用于基于块名称和字段名称生成资产句柄。它根据块是否为核心块(以 'core/' 开头)采用不同的命名规则,并支持索引参数以处理多个资产。

关键要点

  • 函数根据块名称和字段名称生成资产句柄,用于标识脚本、样式等资源。
  • 对于核心块(如 'core/paragraph'),句柄格式为 'wp-block-{block-name}',并根据字段名称添加后缀(如 '-editor'、'-view')。
  • 对于非核心块,句柄格式为 '{block-name}-{field-mapping}',其中字段映射将 metadata 字段(如 'editorScript')转换为连字符格式(如 'editor-script')。
  • 可选参数 $index 用于处理同一字段的多个资产,当 $index > 0 时,在句柄末尾添加索引号(如 '-2')。
  • 函数返回生成的资产句柄字符串,可直接用于注册或入队资源。

代码示例

function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
    if ( str_starts_with( $block_name, 'core/' ) ) {
        $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
        if ( str_starts_with( $field_name, 'editor' ) ) {
            $asset_handle .= '-editor';
        }
        if ( str_starts_with( $field_name, 'view' ) ) {
            $asset_handle .= '-view';
        }
        if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
            $asset_handle .= '-script-module';
        }
        if ( $index > 0 ) {
            $asset_handle .= '-' . ( $index + 1 );
        }
        return $asset_handle;
    }

    $field_mappings = array(
        'editorScript'     => 'editor-script',
        'editorStyle'      => 'editor-style',
        'script'           => 'script',
        'style'            => 'style',
        'viewScript'       => 'view-script',
        'viewScriptModule' => 'view-script-module',
        'viewStyle'        => 'view-style',
    );
    $asset_handle   = str_replace( '/', '-', $block_name ) .
        '-' . $field_mappings[ $field_name ];
    if ( $index > 0 ) {
        $asset_handle .= '-' . ( $index + 1 );
    }
    return $asset_handle;
}

注意事项

  • 函数区分核心块和非核心块,核心块句柄以 'wp-block-' 开头,非核心块使用块名称的连字符版本。
  • 字段名称需匹配预定义的映射数组,否则可能导致错误;支持字段包括 'editorScript'、'viewScriptModule' 等。
  • 索引参数从 0 开始,但输出时加 1(如 $index=0 不添加,$index=1 添加 '-2'),以符合常见命名习惯。
  • 自 WordPress 6.5.0 起支持 'viewScriptModule' 字段,6.1.0 引入 $index 参数,5.5.0 首次引入此函数。

📄 原文内容

Generates the name for an asset based on the name of the block and the field name provided.

Parameters

$block_namestringrequired
Name of the block.
$field_namestringrequired
Name of the metadata field.
$indexintoptional
Index of the asset when multiple items passed.
Default 0.

Return

string Generated asset name for the block’s field.

Source

function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
	if ( str_starts_with( $block_name, 'core/' ) ) {
		$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
		if ( str_starts_with( $field_name, 'editor' ) ) {
			$asset_handle .= '-editor';
		}
		if ( str_starts_with( $field_name, 'view' ) ) {
			$asset_handle .= '-view';
		}
		if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
			$asset_handle .= '-script-module';
		}
		if ( $index > 0 ) {
			$asset_handle .= '-' . ( $index + 1 );
		}
		return $asset_handle;
	}

	$field_mappings = array(
		'editorScript'     => 'editor-script',
		'editorStyle'      => 'editor-style',
		'script'           => 'script',
		'style'            => 'style',
		'viewScript'       => 'view-script',
		'viewScriptModule' => 'view-script-module',
		'viewStyle'        => 'view-style',
	);
	$asset_handle   = str_replace( '/', '-', $block_name ) .
		'-' . $field_mappings[ $field_name ];
	if ( $index > 0 ) {
		$asset_handle .= '-' . ( $index + 1 );
	}
	return $asset_handle;
}

Changelog

Version Description
6.5.0 Added support for viewScriptModule field.
6.1.0 Added $index parameter.
5.5.0 Introduced.