💡 云策文档标注

概述

settings.custom 是 theme.json 中一个独特的自定义属性,允许开发者定义 CSS 自定义属性(CSS 变量)供主题使用。它通过简单的键值对配置,自动生成 CSS 变量,并支持嵌套结构和自动连字符转换。

关键要点

  • settings.custom 接受一个对象,用于存储自定义值,值必须是有效的 CSS 值或嵌套键值对对象。
  • WordPress 会自动将 settings.custom 中的键值对转换为 CSS 自定义属性,命名模式为 --wp--custom--{key}--{value}。
  • 键名中的驼峰命名(如 lineHeight)会自动转换为连字符形式(如 line-height),数字也会被处理。
  • 支持嵌套属性,可创建多级 CSS 变量,但嵌套过深会导致变量名变长。
  • 生成的 CSS 自定义属性需在 theme.json 的 styles 部分或外部 CSS 文件中引用才能生效,例如使用 var:custom|key|subkey 或 var( --wp--custom--key--subkey ) 语法。

代码示例

{
  "version": 2,
  "settings": {
    "custom": {
      "lineHeight": {
        "xs": "1",
        "sm": "1.25",
        "md": "1.5",
        "lg": "1.75"
      }
    }
  },
  "styles": {
    "typography": {
      "lineHeight": "var:custom|line-height|md"
    },
    "blocks": {
      "core/paragraph": {
        "typography": {
          "lineHeight": "var:custom|line-height|lg"
        }
      }
    }
  }
}

注意事项

  • CSS 自定义属性本身不生效,必须在 CSS 或 theme.json 的 styles 中引用才能应用样式。
  • 在 theme.json 中引用时,可使用 var:custom|key|subkey 格式或直接使用 CSS 变量名 var( --wp--custom--key--subkey )。
  • 嵌套结构无限制,但应避免过度嵌套以保持变量名简洁。

📄 原文内容

The settings.custom property is unique among other settings in theme.json. As its name implies, it is a custom property. This means that you get to decide how to use it. Essentially, it provides a method for creating CSS custom properties that you might need elsewhere in your theme.

In this document, you will learn what the custom property is for and how you can use it in your theme.

Overview of the custom setting

The settings.custom property accepts a single object, and this object can be used to store other values. The individual object values must be valid CSS values or an object with nested key/value pairs.

Here is an example snippet from theme.json with no custom values set:

{
	"version": 2,
	"settings": {
		"custom": {}
	}
}

The great thing about the settings.custom object is that you can use it to create your own CSS custom properties. When you add a key and value to the object, WordPress will automatically generate the CSS custom property, assign the value, and load it for you.

The generated CSS custom property will follow this pattern: --wp--custom--{key}--{value}.

Suppose you wanted to use the key of fruit and give it a value of apple. Add this to your theme.json file:

{
	"version": 2,
	"settings": {
		"custom": {
			"fruit": "apple"
		}
	}
}

WordPress will then generate this CSS:

body {
	--wp--custom--fruit: apple;
}

How CSS custom properties are generated

As you learned above, the settings.custom.fruit key name will generate the --wp--custom--fruit variable in CSS. But there are other cases too.

Automatic hyphenation

WordPress will automatically hyphenate camel-cased names. For example, lineHeight in the following example will become line-height:

{
	"version": 2,
	"settings": {
		"custom": {
			"lineHeight": "1.4em"
		}
	}
}

This will create the following CSS:

body {
	--wp--custom--line-height: 1.4em;
}

Numbers are handled the same as uppercase letters when used as a key. For example, a key of abc123 will become abc-1-2-3 in the resulting CSS.

Nested properties

Building off the above example, suppose you wanted to create several line-height CSS custom properties for use in your theme. For this, you might want to create an object under settings.custom.lineHeight instead of a single value.

Add the following to your theme.json file:

{
	"version": 2,
	"settings": {
		"custom": {
			"lineHeight": {
				"xs": "1",
				"sm": "1.25",
				"md": "1.5",
				"lg": "1.75"
			}
		}
	}
}

WordPress will automatically use this nested structure when generating the CSS custom property names.

This will generate this CSS:

body {
	--wp--custom--line-height--xs: 1;
	--wp--custom--line-height--sm: 1.25;
	--wp--custom--line-height--md: 1.5;
	--wp--custom--line-height--lg: 1.75;
}

There is no limit to the amount of nesting you can do, but keep in mind that the more you nest, the longer your CSS custom property names become.

Practical usage

What you use the settings.custom property for is entirely up to you. At its core, all it really does is generate CSS custom properties, which don’t do anything on their own. Custom properties must also be used in CSS.

In the previous theme.json example above, you created a set of line-heights. There are a number of ways you can put these into practical use. 

Use in theme.json styles

In the Styles documentation, you will learn how to apply styles to the root element, elements, and blocks via theme.json. This will be one of the primary use cases for integrating with settings.custom.

Suppose you wanted to register the same set of line-heights from above and make use of them. Maybe you want to set the root element to the md line-height and Paragraph blocks to lg. You can access each line-height property via var:custom|line-height|md and var:custom|line-height|lg, respectively.

Use this code in your theme.json file:

{
	"version": 2,
	"settings": {
		"custom": {
			"lineHeight": {
				"xs": "1",
				"sm": "1.25",
				"md": "1.5",
				"lg": "1.75"
			}
		}
	},
	"styles": {
		"typography": {
			"lineHeight": "var:custom|line-height|md"
		}
		"blocks": {
			"core/paragraph": {
				"typography": {
					"lineHeight": "var:custom|line-height|lg"
				}
			}
		}
	}
}

You can also reference the values via their CSS custom properties. For example, instead of using var:custom|line-height|md, use var( --wp--custom--line-height--md ).

Remember, you will learn more about styling via theme.json from the Styles documentation. You can use what you learn there to combine with the techniques outlined here.

Use in CSS

There are times when you might need to reference the generated CSS custom properties directly in CSS, such as your style.css file. To do this, you must use the CSS custom property name.

Suppose you needed to target a class with the name of .example-class and to give it the sm line-height that you’ve registered. Use this code in your CSS:

.example-class {
	line-height: var( --wp--custom--line-height--sm );
}