函数文档

wp_style_loader_src()

💡 云策文档标注

概述

wp_style_loader_src() 是一个 WordPress 函数,用于管理后台管理屏幕的 CSS 样式表加载。它根据用户设置的颜色方案和安装模式动态调整 CSS 文件的 URL。

关键要点

  • 函数主要用于处理后台管理屏幕的 CSS 样式表源 URL,特别是颜色相关的样式。
  • 在 WordPress 安装模式下,会将 'wp-admin/' 路径替换为 './'。
  • 通过全局变量 $_wp_admin_css_colors 和用户选项 'admin_color' 来确定要加载的 CSS 文件。
  • 返回调整后的 URL 字符串,或在无法获取时返回 false。

代码示例

function wp_style_loader_src( $src, $handle ) {
	global $_wp_admin_css_colors;

	if ( wp_installing() ) {
		return preg_replace( '#^wp-admin/#', './', $src );
	}

	if ( 'colors' === $handle ) {
		$color = get_user_option( 'admin_color' );

		if ( empty( $color ) || ! isset( $_wp_admin_css_colors[ $color ] ) ) {
			$color = 'fresh';
		}

		$color = $_wp_admin_css_colors[ $color ];
		$url   = $color->url;

		if ( ! $url ) {
			return false;
		}

		$parsed = parse_url( $src );
		if ( isset( $parsed['query'] ) && $parsed['query'] ) {
			wp_parse_str( $parsed['query'], $qv );
			$url = add_query_arg( $qv, $url );
		}

		return $url;
	}

	return $src;
}

注意事项

  • 函数仅对 handle 为 'colors' 或 'colors-rtl' 的样式表生效,其他 handle 会直接返回原 $src。
  • 依赖全局变量 $_wp_admin_css_colors,开发者需确保其已正确初始化。
  • 在自定义开发中,可参考此函数逻辑来修改或扩展后台样式加载行为。

📄 原文内容

Administration Screen CSS for changing the styles.

Description

If installing the ‘wp-admin/’ directory will be replaced with ‘./’.

The $_wp_admin_css_colors global manages the Administration Screens CSS stylesheet that is loaded. The option that is set is ‘admin_color’ and is the color and key for the array. The value for the color key is an object with a ‘url’ parameter that has the URL path to the CSS file.

The query from $src parameter will be appended to the URL that is given from the $_wp_admin_css_colors array value URL.

Parameters

$srcstringrequired
Source URL.
$handlestringrequired
Either 'colors' or 'colors-rtl'.

Return

string|false URL path to CSS stylesheet for Administration Screens.

Source

function wp_style_loader_src( $src, $handle ) {
	global $_wp_admin_css_colors;

	if ( wp_installing() ) {
		return preg_replace( '#^wp-admin/#', './', $src );
	}

	if ( 'colors' === $handle ) {
		$color = get_user_option( 'admin_color' );

		if ( empty( $color ) || ! isset( $_wp_admin_css_colors[ $color ] ) ) {
			$color = 'fresh';
		}

		$color = $_wp_admin_css_colors[ $color ];
		$url   = $color->url;

		if ( ! $url ) {
			return false;
		}

		$parsed = parse_url( $src );
		if ( isset( $parsed['query'] ) && $parsed['query'] ) {
			wp_parse_str( $parsed['query'], $qv );
			$url = add_query_arg( $qv, $url );
		}

		return $url;
	}

	return $src;
}

Changelog

Version Description
2.6.0 Introduced.