generate_block_asset_handle

The timeline below displays how wordpress function generate_block_asset_handle has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 * @since 6.5.0 Added support for `viewScriptModule` field.
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @param int    $index      Optional. Index of the asset when multiple items passed.
 *                           Default 0.
 * @return string Generated asset name for the block's field.
 */
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;
}

WordPress Version: 6.3

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @param int    $index      Optional. Index of the asset when multiple items passed.
 *                           Default 0.
 * @return string Generated asset name for the block's field.
 */
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 ($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;
}

WordPress Version: 6.1

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @param int    $index      Optional. Index of the asset when multiple items passed.
 *                           Default 0.
 * @return string Generated asset name for the block's field.
 */
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;
}

WordPress Version: 5.9

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @return string Generated asset name for the block's field.
 */
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];
}

WordPress Version: 5.7

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @return string Generated asset name for the block's field.
 */
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';
        }
        return $asset_handle;
    }
    $field_mappings = array('editorScript' => 'editor-script', 'script' => 'script', 'editorStyle' => 'editor-style', 'style' => 'style');
    return str_replace('/', '-', $block_name) . '-' . $field_mappings[$field_name];
}

WordPress Version: 5.5

/**
 * Generates the name for an asset based on the name of the block
 * and the field name provided.
 *
 * @since 5.5.0
 *
 * @param string $block_name Name of the block.
 * @param string $field_name Name of the metadata field.
 * @return string Generated asset name for the block's field.
 */
function generate_block_asset_handle($block_name, $field_name)
{
    $field_mappings = array('editorScript' => 'editor-script', 'script' => 'script', 'editorStyle' => 'editor-style', 'style' => 'style');
    return str_replace('/', '-', $block_name) . '-' . $field_mappings[$field_name];
}