generate_block_asset_handle

函式
generate_block_asset_handle ( $block_name, $field_name, $index = 0 )
引數
  • (string) $block_name Name of the block.
    Required:
  • (string) $field_name Name of the metadata field.
    Required:
  • (int) $index Optional. Index of the asset when multiple items passed. Default 0.
    Required:
返回值
  • (string) Generated asset name for the block's field.
定義位置
相關方法
register_block_style_handleregister_block_script_handlerender_block_core_filewp_generate_block_templates_export_filewp_enqueue_block_style
引入
5.5.0
棄用
-

generate_block_asset_handle: 這個函式用來為一個區塊生成一個唯一的資產控制代碼。這個控制代碼用於識別與該塊相關的資產,例如它的指令碼和樣式檔案。

根據區塊的名稱和提供的欄位名生成資產的名稱。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >