wpseek.com
A WordPress-centric search engine for devs and theme authors



generate_block_asset_handle › WordPress Function

Since5.5.0
Deprecatedn/a
generate_block_asset_handle ( $block_name, $field_name, $index = 0 )
Parameters: (3)
  • (string) $block_name Name of the block.
    Required: Yes
  • (string) $field_name Name of the metadata field.
    Required: Yes
  • (int) $index Optional. Index of the asset when multiple items passed. Default 0.
    Required: No
    Default:
Returns:
  • (string) Generated asset name for the block's field.
Defined at:
Codex:
Change Log:
  • 6.1.0

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



Source

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;
}