💡 云策文档标注

概述

本文档介绍了如何在 theme.json 中配置块级设置,以覆盖全局设置,实现针对特定块的样式控制。通过 settings.blocks 属性,开发者可以为单个块(如核心的 Cover 块)定义独立的颜色、排版、间距等属性。

关键要点

  • theme.json 的 settings.block 属性允许在块级别配置属性,如颜色、排版和间距,这些设置会覆盖全局设置。
  • 块级设置使用 settings.blocks[namespace/slug] 结构,例如 settings.blocks[core/cover].color.palette 来为 Cover 块定义自定义调色板。
  • WordPress 默认在 theme.json 中为某些块(如 Button 和 Pullquote)启用了块级设置,开发者可以覆盖这些默认设置以实现自定义行为。
  • 配置块级设置前,需熟悉全局 theme.json 设置,并了解目标块的 namespace 和 slug(可从 block.json 文件获取)。

代码示例

{
  "version": 2,
  "settings": {
    "color": {
      "palette": [
        {
          "color": "#0284c7",
          "name": "Base",
          "slug": "base"
        },
        {
          "color": "#ffffff",
          "name": "Contrast",
          "slug": "contrast"
        }
      ]
    },
    "blocks": {
      "core/cover": {
        "color": {
          "palette": [
            {
              "color": "#ea580c",
              "name": "Base",
              "slug": "base"
            },
            {
              "color": "#fff7ed",
              "name": "Contrast",
              "slug": "contrast"
            }
          ]
        }
      }
    }
  }
}

注意事项

  • 块级设置会覆盖全局设置,因此需谨慎配置以避免意外样式冲突。
  • 如果全局设置未应用于某些块(如 Button 或 Pullquote),可能是因为存在默认的块级设置,需要在 theme.json 中显式覆盖。
  • 确保使用正确的 namespace 和 slug 组合来定位块,例如 core/cover 表示核心的 Cover 块。

📄 原文内容

Most settings in theme.json let you configure properties at the global level. This means that they apply to all blocks that support them. But WordPress also lets you control this at the block level.

That’s what the settings.block property is for—you can configure everything from typography, to colors, to spacing, and more for individual blocks.

Before moving forward with this page of the handbook, it is important to familiarize yourself with at least some of the existing theme.json settings. This way, you can apply them on a per-block basis.

How block settings work

In the previous pages of the theme.json settings documentation, you learned how to configure specific properties at the global level. Essentially, this means that they are applied to all blocks that support the specific property/feature.

You would have configured many of these properties in your theme.json (shortened for example—you can review all available properties via the main settings documentation):

{
	"version": 2,
	"settings": {
		"border": {},
		"color": {},
		"custom": {},
		"spacing": {},
		"typography": {}
	}
}

But there are times when you need to add settings at the individual block level, and anything set for the block will overrule its global setting. So let’s explore an example that shows block-specific settings overwriting the global settings.

For this example, you will create a custom color palette, which you can learn about in the color settings documentation. This will be applied globally and used for every block’s color picker. Then, you will create a custom color palette for the Cover block that’s only shown for the Cover block.

First, add your global color palette in theme.json with two colors named base and contrast:

{
	"version": 2,
	"settings": {
		"color": {
			"palette": [
				{
					"color": "#0284c7",
					"name": "Base",
					"slug": "base"
				},
				{
					"color": "#ffffff",
					"name": "Contrast",
					"slug": "contrast"
				}
			]
		}
	}
}

Now add a Cover block and some nested text in the WordPress editor, saving the Text and Overlay colors for the block to your two custom colors:

WordPress post editor showing a Cover block with a blue background and white text.

As you can see, the Cover block currently uses the global color palette that you configured in your theme.json file.

Suppose that you wanted the Cover block to use an orange color palette. You can configure that by targeting settings.blocks[core/cover].color.palette in your theme.json file and passing an array of custom colors.

Add this new section to your existing theme.json so that it looks like this:

{
	"version": 2,
	"settings": {
		"color": {
			"palette": [
				{
					"color": "#0284c7",
					"name": "Base",
					"slug": "base"
				},
				{
					"color": "#ffffff",
					"name": "Contrast",
					"slug": "contrast"
				}
			]
		}
		"blocks": {
			"core/cover": {
				"color": {
					"palette": [
						{
							"color": "#ea580c",
							"name": "Base",
							"slug": "base"
						},
						{
							"color": "#fff7ed",
							"name": "Contrast",
							"slug": "contrast"
						}
					]
				}
			}
		}
	}
}

As shown in the theme.json example, you use the same organizational structure for block settings as you do for global settings (i.e., settings.color at the global level but settings.blocks[core/cover].color at the block level).

If you refresh your browser window, your Cover block should now show the new colors:

WordPress post editor showing a Cover block with an orange background and white text.

If you check other blocks, they will still use the global color palette. Only the Cover block will use this custom orange palette.

Per-block color palettes are just the tip of the iceberg. You can configure any theme.json for any block (assuming the block supports it). This gives you an incredible amount of control over how your theme works.

When targeting a block’s settings, you must know both its namespace and slug. Above, you learned that the Cover block has the namespace (core) and slug (cover), creating the namespace/slug combination of core/cover. All core WordPress blocks have the core namespace, and you can find this information for any block (including from third-party plugins) in its block.json file.

Default block settings

Believe it or not, WordPress actually configures a couple of default block settings in theme.json. Generally, this would be left to themes, but these settings are primarily enabled for backward compatibility with features from older versions of WordPress.

WordPress enables a few settings for the Button and Pullquote blocks by default. Here is what this looks like in the default theme.json:

{
	"version": 2,
	"settings": {
		"blocks": {
			"core/button": {
				"border": {
					"radius": true
				}
			},
			"core/pullquote": {
				"border": {
					"color": true,
					"radius": true,
					"style": true,
					"width": true
				}
			}
		}
	}
}

You can overwrite these block-specific settings in your theme.json file just as you learned to do in the previous section of this documentation.

If you are wondering why some of your global settings do not seem to apply to certain blocks, particularly Button and Pullquote, it is likely because they are being set at the block level. You will need to override these in settings.blocks in your theme.json if you want a different behavior.