函数文档

remove_block_asset_path_prefix()

💡 云策文档标注

概述

remove_block_asset_path_prefix() 函数用于移除块资产路径的前缀,如果提供了前缀的话。它处理资产句柄或带前缀的路径,返回去除前缀后的路径或原始值。

关键要点

  • 函数接受一个参数 $asset_handle_or_path,可以是资产句柄或带前缀的路径。
  • 如果路径以 'file:' 前缀开头,则移除该前缀;如果移除后以 './' 开头,则进一步移除 './'。
  • 返回处理后的路径或原始值,确保路径格式正确。

代码示例

function remove_block_asset_path_prefix( $asset_handle_or_path ) {
	$path_prefix = 'file:';
	if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
		return $asset_handle_or_path;
	}
	$path = substr(
		$asset_handle_or_path,
		strlen( $path_prefix )
	);
	if ( str_starts_with( $path, './' ) ) {
		$path = substr( $path, 2 );
	}
	return $path;
}

📄 原文内容

Removes the block asset’s path prefix if provided.

Parameters

$asset_handle_or_pathstringrequired
Asset handle or prefixed path.

Return

string Path without the prefix or the original value.

Source

function remove_block_asset_path_prefix( $asset_handle_or_path ) {
	$path_prefix = 'file:';
	if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
		return $asset_handle_or_path;
	}
	$path = substr(
		$asset_handle_or_path,
		strlen( $path_prefix )
	);
	if ( str_starts_with( $path, './' ) ) {
		$path = substr( $path, 2 );
	}
	return $path;
}

Changelog

Version Description
5.5.0 Introduced.