函数文档

get_block_asset_url()

💡 云策文档标注

概述

get_block_asset_url() 函数用于获取块(block)资产的 URL。它接受一个规范化路径作为参数,并根据路径位置返回相应的 URL,失败时返回 false。

关键要点

  • 参数 $path 必须是规范化路径,用于指定块资产的位置。
  • 函数返回块资产的 URL 字符串,如果路径为空或处理失败则返回 false。
  • 函数内部处理逻辑包括:检查路径是否在 WordPress 核心 includes 目录、主题目录或子主题目录中,并相应调用 includes_url()、get_theme_file_uri() 或 plugins_url() 来生成 URL。
  • 使用静态变量缓存规范化路径以提高性能。

代码示例

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( realpath( 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( realpath( 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 );
}

注意事项

  • 路径必须规范化,以确保在不同操作系统(如 Windows)上正常工作。
  • 函数优先检查 WordPress 核心 includes 目录,然后是主题目录和子主题目录,最后默认处理为插件目录。
  • 相关函数如 wp_normalize_path()、includes_url()、get_theme_file_uri() 和 plugins_url() 用于辅助路径处理和 URL 生成。
  • 该函数自 WordPress 6.4.0 版本引入。

📄 原文内容

Gets the URL to a block asset.

Parameters

$pathstringrequired
A normalized path to a block asset.

Return

string|false The URL to the block asset or false on failure.

Source

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( realpath( 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( realpath( 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 );
}

Changelog

Version Description
6.4.0 Introduced.