Skip to content

Commit 1813b51

Browse files
authored
theme.json docs: add examples and highlight backward compatibility with add_theme_support (#33421)
1 parent a5e89d4 commit 1813b51

File tree

1 file changed

+98
-22
lines changed

1 file changed

+98
-22
lines changed

docs/how-to-guides/themes/theme-json.md

Lines changed: 98 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ WordPress 5.8 comes with [a new mechanism](https://make.wordpress.org/core/2021/
1010
- Specification
1111
- version
1212
- settings
13+
- Backward compatibility with add_theme_support
1314
- Presets
1415
- Custom
16+
- Setting examples
1517
- styles
1618
- Top-level
1719
- Block-level
@@ -269,6 +271,10 @@ The settings section has the following structure:
269271

270272
Each block can configure any of these settings separately, providing a more fine-grained control over what exists via `add_theme_support`. The settings declared at the top-level affect to all blocks, unless a particular block overwrites it. It's a way to provide inheritance and configure all blocks at once.
271273

274+
Note, however, that not all settings are relevant for all blocks. The settings section provides an opt-in/opt-out mechanism for themes, but it's the block's responsibility to add support for the features that are relevant to it. For example, if a block doesn't implement the `dropCap` feature, a theme can't enable it for such a block through `theme.json`.
275+
276+
#### Backward compatibility with add_theme_support
277+
272278
To retain backward compatibility, the existing `add_theme_support` declarations that configure the block editor are retrofit in the proper categories for the top-level section. For example, if a theme uses `add_theme_support('disable-custom-colors')`, it'll be the same as setting `settings.color.custom` to `false`. If the `theme.json` contains any settings, these will take precedence over the values declared via `add_theme_support`. This is the complete list of equivalences:
273279

274280
| add_theme_support | theme.json setting |
@@ -284,28 +290,6 @@ To retain backward compatibility, the existing `add_theme_support` declarations
284290
| `editor-gradient-presets` | Provide the list of gradients via `color.gradients`. |
285291
| `experimental-link-color` | Set `color.link` to `true`. |
286292

287-
Let's say a theme author wants to enable custom colors only for the paragraph block. This is how it can be done:
288-
289-
```json
290-
{
291-
"version": 1,
292-
"settings": {
293-
"color": {
294-
"custom": false
295-
},
296-
"blocks": {
297-
"core/paragraph": {
298-
"color": {
299-
"custom": true
300-
}
301-
}
302-
}
303-
}
304-
}
305-
```
306-
307-
Note, however, that not all settings are relevant for all blocks. The settings section provides an opt-in/opt-out mechanism for themes, but it's the block's responsibility to add support for the features that are relevant to it. For example, if a block doesn't implement the `dropCap` feature, a theme can't enable it for such a block through `theme.json`.
308-
309293
#### Presets
310294

311295
Presets are part of the settings section. They are values that are shown to the user via some UI controls. By defining them via `theme.json` the engine can do more for themes, such as automatically translate the preset name or enqueue the corresponding CSS classes and custom properties.
@@ -511,6 +495,98 @@ body {
511495

512496
Note that the name of the variable is created by adding `--` in between each nesting level and `camelCase` fields are transformed to `kebab-case`.
513497

498+
#### Settings examples
499+
500+
- Enable custom colors only for the paragraph block:
501+
502+
```json
503+
{
504+
"version": 1,
505+
"settings": {
506+
"color": {
507+
"custom": false
508+
},
509+
"blocks": {
510+
"core/paragraph": {
511+
"color": {
512+
"custom": true
513+
}
514+
}
515+
}
516+
}
517+
}
518+
```
519+
520+
- Disable border radius for the button block (borders only work in the plugin so far):
521+
522+
```json
523+
{
524+
"version": 1,
525+
"settings": {
526+
"core/button": {
527+
"border": {
528+
"customRadius": false
529+
}
530+
}
531+
}
532+
}
533+
```
534+
535+
- Provide the group block a different palette than the rest:
536+
537+
```json
538+
{
539+
"version": 1,
540+
"settings": {
541+
"color": {
542+
"palette": [
543+
{
544+
"slug": "black",
545+
"color": "#000000",
546+
"name": "Black"
547+
},
548+
{
549+
"slug": "white",
550+
"color": "#FFFFFF",
551+
"name": "White"
552+
},
553+
{
554+
"slug": "red",
555+
"color": "#FF0000",
556+
"name": "Red"
557+
},
558+
{
559+
"slug": "green",
560+
"color": "#00FF00",
561+
"name": "Green"
562+
},
563+
{
564+
"slug": "blue",
565+
"color": "#0000FF",
566+
"name": "Blue"
567+
}
568+
]
569+
},
570+
"core/group": {
571+
"color": {
572+
"palette": [
573+
{
574+
"slug": "black",
575+
"color": "#000000",
576+
"name": "Black"
577+
},
578+
{
579+
"slug": "white",
580+
"color": "#FFF",
581+
"name": "White"
582+
}
583+
]
584+
}
585+
}
586+
}
587+
}
588+
```
589+
514590
### Styles
515591

516592
<div class="callout callout-alert">

0 commit comments

Comments
 (0)