函数文档

get_theme_root_uri()

💡 云策文档标注

概述

get_theme_root_uri() 函数用于检索主题目录的 URI,返回的字符串不包含尾部斜杠。它支持通过参数指定主题样式表或模板名称,并基于主题根目录计算 URI。

关键要点

  • 函数返回主题目录的 URI,无尾部斜杠。
  • 参数 $stylesheet_or_template 可选,用于指定主题的样式表或模板名称,默认使用主主题根目录。
  • 参数 $theme_root 可选,提供主题根目录以避免调用 get_raw_theme_root(),默认为空。
  • 返回值类型为字符串,表示主题目录的 URI。
  • 函数内部逻辑包括:根据参数判断主题根目录,处理绝对路径转换为 URI,并应用 'theme_root_uri' 过滤器。
  • 相关函数包括 get_raw_theme_root()、content_url()、plugins_url()、site_url()、apply_filters() 和 get_option()。
  • 被 get_stylesheet_directory_uri()、get_template_directory_uri() 和 WP_Theme::get_theme_root_uri() 使用。
  • 自 WordPress 1.5.0 版本引入。

代码示例

function get_theme_root_uri( $stylesheet_or_template = '', $theme_root = '' ) {
    global $wp_theme_directories;

    if ( $stylesheet_or_template && ! $theme_root ) {
        $theme_root = get_raw_theme_root( $stylesheet_or_template );
    }

    if ( $stylesheet_or_template && $theme_root ) {
        if ( in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
            // Absolute path. Make an educated guess. YMMV -- but note the filter below.
            if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) {
                $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
            } elseif ( str_starts_with( $theme_root, ABSPATH ) ) {
                $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
            } elseif ( str_starts_with( $theme_root, WP_PLUGIN_DIR ) || str_starts_with( $theme_root, WPMU_PLUGIN_DIR ) ) {
                $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
            } else {
                $theme_root_uri = $theme_root;
            }
        } else {
            $theme_root_uri = content_url( $theme_root );
        }
    } else {
        $theme_root_uri = content_url( 'themes' );
    }

    /**
     * Filters the URI for themes directory.
     *
     * @since 1.5.0
     *
     * @param string $theme_root_uri         The URI for themes directory.
     * @param string $siteurl                WordPress web address which is set in General Options.
     * @param string $stylesheet_or_template The stylesheet or template name of the theme.
     */
    return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
}

注意事项

  • 函数返回的 URI 不包含尾部斜杠,使用时需注意拼接路径。
  • 参数 $theme_root 可用于优化性能,避免额外调用 get_raw_theme_root()。
  • 内部路径转换逻辑基于常见目录结构(如 WP_CONTENT_DIR、ABSPATH),自定义主题根目录可能需要调整。
  • 可通过 'theme_root_uri' 过滤器修改返回的 URI,参数包括 $theme_root_uri、$siteurl 和 $stylesheet_or_template。

📄 原文内容

Retrieves URI for themes directory.

Description

Does not have trailing slash.

Parameters

$stylesheet_or_templatestringoptional
The stylesheet or template name of the theme.
Default is to leverage the main theme root.
$theme_rootstringoptional
The theme root for which calculations will be based, preventing the need for a get_raw_theme_root() call. Default empty.

Return

string Themes directory URI.

Source

function get_theme_root_uri( $stylesheet_or_template = '', $theme_root = '' ) {
	global $wp_theme_directories;

	if ( $stylesheet_or_template && ! $theme_root ) {
		$theme_root = get_raw_theme_root( $stylesheet_or_template );
	}

	if ( $stylesheet_or_template && $theme_root ) {
		if ( in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
			// Absolute path. Make an educated guess. YMMV -- but note the filter below.
			if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) {
				$theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
			} elseif ( str_starts_with( $theme_root, ABSPATH ) ) {
				$theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
			} elseif ( str_starts_with( $theme_root, WP_PLUGIN_DIR ) || str_starts_with( $theme_root, WPMU_PLUGIN_DIR ) ) {
				$theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
			} else {
				$theme_root_uri = $theme_root;
			}
		} else {
			$theme_root_uri = content_url( $theme_root );
		}
	} else {
		$theme_root_uri = content_url( 'themes' );
	}

	/**
	 * Filters the URI for themes directory.
	 *
	 * @since 1.5.0
	 *
	 * @param string $theme_root_uri         The URI for themes directory.
	 * @param string $siteurl                WordPress web address which is set in General Options.
	 * @param string $stylesheet_or_template The stylesheet or template name of the theme.
	 */
	return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
}

Hooks

apply_filters( ‘theme_root_uri’, string $theme_root_uri, string $siteurl, string $stylesheet_or_template )

Filters the URI for themes directory.

Changelog

Version Description
1.5.0 Introduced.