get_block_asset_url

函式
get_block_asset_url ( $path )
引數
  • (string) $path A normalized path to a block asset.
    Required:
返回值
  • (string|false) The URL to the block asset or false on failure.
定義位置
相關方法
generate_block_asset_handleget_locale_stylesheet_uriget_block_categoriesget_rest_urlget_blog_status
引入
6.4.0
棄用
-

獲取區塊資產的 URL。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_block_asset_url( $path ) {
if ( empty( $path ) ) {
return false;
}
// Path needs to be normalized to work in Windows env.
static $wpinc_path_norm = '';
if ( ! $wpinc_path_norm ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
}
if ( str_starts_with( $path, $wpinc_path_norm ) ) {
return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
}
static $template_paths_norm = array();
$template = get_template();
if ( ! isset( $template_paths_norm[ $template ] ) ) {
$template_paths_norm[ $template ] = wp_normalize_path( get_template_directory() );
}
if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
}
if ( is_child_theme() ) {
$stylesheet = get_stylesheet();
if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
$template_paths_norm[ $stylesheet ] = wp_normalize_path( get_stylesheet_directory() );
}
if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
}
}
return plugins_url( basename( $path ), $path );
}
function get_block_asset_url( $path ) { if ( empty( $path ) ) { return false; } // Path needs to be normalized to work in Windows env. static $wpinc_path_norm = ''; if ( ! $wpinc_path_norm ) { $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); } if ( str_starts_with( $path, $wpinc_path_norm ) ) { return includes_url( str_replace( $wpinc_path_norm, '', $path ) ); } static $template_paths_norm = array(); $template = get_template(); if ( ! isset( $template_paths_norm[ $template ] ) ) { $template_paths_norm[ $template ] = wp_normalize_path( get_template_directory() ); } if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) { return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) ); } if ( is_child_theme() ) { $stylesheet = get_stylesheet(); if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) { $template_paths_norm[ $stylesheet ] = wp_normalize_path( get_stylesheet_directory() ); } if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) { return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) ); } } return plugins_url( basename( $path ), $path ); }
function get_block_asset_url( $path ) {
	if ( empty( $path ) ) {
		return false;
	}

	// Path needs to be normalized to work in Windows env.
	static $wpinc_path_norm = '';
	if ( ! $wpinc_path_norm ) {
		$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
	}

	if ( str_starts_with( $path, $wpinc_path_norm ) ) {
		return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
	}

	static $template_paths_norm = array();

	$template = get_template();
	if ( ! isset( $template_paths_norm[ $template ] ) ) {
		$template_paths_norm[ $template ] = wp_normalize_path( get_template_directory() );
	}

	if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
		return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
	}

	if ( is_child_theme() ) {
		$stylesheet = get_stylesheet();
		if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
			$template_paths_norm[ $stylesheet ] = wp_normalize_path( get_stylesheet_directory() );
		}

		if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
			return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
		}
	}

	return plugins_url( basename( $path ), $path );
}

常見問題

FAQs
檢視更多 >