generate_block_asset_handle() WordPress Function

The generate_block_asset_handle() function is used to generate a unique handle for a given asset. This is useful for caching purposes, as well as for ensuring that assets are only included once in a given page.

generate_block_asset_handle( string $block_name, string $field_name ) #

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


Parameters

$block_name

(string)(Required)Name of the block.

$field_name

(string)(Required)Name of the metadata field.


Top ↑

Return

(string) Generated asset name for the block's field.


Top ↑

Source

File: wp-includes/blocks.php

function generate_block_asset_handle( $block_name, $field_name ) {
	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';
		}
		return $asset_handle;
	}

	$field_mappings = array(
		'editorScript' => 'editor-script',
		'script'       => 'script',
		'viewScript'   => 'view-script',
		'editorStyle'  => 'editor-style',
		'style'        => 'style',
	);
	return str_replace( '/', '-', $block_name ) .
		'-' . $field_mappings[ $field_name ];
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.