Using WordPress ‘remove_block_asset_path_prefix()’ PHP function

The remove_block_asset_path_prefix() WordPress PHP function removes the block asset’s path prefix if provided.

Usage

remove_block_asset_path_prefix( $asset_handle_or_path );

Example:

Input: remove_block_asset_path_prefix( 'path_prefix/block_style' );

Output: 'block_style'

Parameters

  • $asset_handle_or_path (string) - Asset handle or prefixed path. Required parameter.

More information

See WordPress Developer Resources: remove_block_asset_path_prefix

Examples

Removing path prefix from a block style

This example demonstrates how to remove the path prefix from a block style.

function my_theme_remove_block_style_prefix() {
    $style_with_prefix = 'my-theme/block-style';
    $style_without_prefix = remove_block_asset_path_prefix( $style_with_prefix );
    // $style_without_prefix will be 'block-style'
}

Using remove_block_asset_path_prefix with enqueue_block_styles_assets

This example shows how to use the function when enqueuing block styles.

function my_theme_enqueue_block_styles() {
    $style_with_prefix = 'my-theme/block-style';
    $style_without_prefix = remove_block_asset_path_prefix( $style_with_prefix );
    wp_enqueue_style( $style_without_prefix, get_template_directory_uri() . '/styles/' . $style_without_prefix . '.css', array(), '1.0.0' );
}
add_action( 'enqueue_block_assets', 'my_theme_enqueue_block_styles' );

Removing path prefix from a block script

This example demonstrates how to remove the path prefix from a block script.

function my_theme_remove_block_script_prefix() {
    $script_with_prefix = 'my-theme/block-script';
    $script_without_prefix = remove_block_asset_path_prefix( $script_with_prefix );
    // $script_without_prefix will be 'block-script'
}

Using remove_block_asset_path_prefix with enqueue_block_editor_assets

This example shows how to use the function when enqueuing block editor scripts.

function my_theme_enqueue_block_editor_scripts() {
    $script_with_prefix = 'my-theme/block-editor-script';
    $script_without_prefix = remove_block_asset_path_prefix( $script_with_prefix );
    wp_enqueue_script( $script_without_prefix, get_template_directory_uri() . '/scripts/' . $script_without_prefix . '.js', array(), '1.0.0' );
}
add_action( 'enqueue_block_editor_assets', 'my_theme_enqueue_block_editor_scripts' );

Removing path prefix from a block asset within a loop

This example demonstrates how to use the function to remove path prefixes from multiple block assets.

function my_theme_remove_multiple_block_asset_prefixes() {
    $block_assets = array(
        'my-theme/block-style-one',
        'my-theme/block-style-two',
        'my-theme/block-script'
    );

    foreach ( $block_assets as $asset ) {
        $asset_without_prefix = remove_block_asset_path_prefix( $asset );
        // The loop will remove the 'my-theme/' prefix from each block asset
    }
}