函数文档

wp_enqueue_global_styles_css_custom_properties()

💡 云策文档标注

概述

wp_enqueue_global_styles_css_custom_properties() 函数用于将来自 theme.json 的 CSS 自定义属性(CSS Custom Properties)入队到 WordPress 中。它通过注册一个空样式表并添加内联样式来实现。

关键要点

  • 函数作用:入队从 theme.json 生成的 CSS 自定义属性,以便在主题中全局使用。
  • 实现方式:使用 wp_register_style() 注册一个空样式表,然后通过 wp_add_inline_style() 添加由 wp_get_global_stylesheet() 获取的变量样式,最后用 wp_enqueue_style() 入队。
  • 相关函数:涉及 wp_get_global_stylesheet()、wp_register_style()、wp_add_inline_style() 和 wp_enqueue_style(),用于合并核心、主题和用户数据并管理样式。
  • 版本历史:自 WordPress 5.9.0 版本引入。

代码示例

function wp_enqueue_global_styles_css_custom_properties() {
	wp_register_style( 'global-styles-css-custom-properties', false );
	wp_add_inline_style( 'global-styles-css-custom-properties', wp_get_global_stylesheet( array( 'variables' ) ) );
	wp_enqueue_style( 'global-styles-css-custom-properties' );
}

📄 原文内容

Function that enqueues the CSS Custom Properties coming from theme.json.

Source

function wp_enqueue_global_styles_css_custom_properties() {
	wp_register_style( 'global-styles-css-custom-properties', false );
	wp_add_inline_style( 'global-styles-css-custom-properties', wp_get_global_stylesheet( array( 'variables' ) ) );
	wp_enqueue_style( 'global-styles-css-custom-properties' );
}

Changelog

Version Description
5.9.0 Introduced.