Theme.json

theme.json is a file that lives in the theme root directory of a theme and provides a canonical way to define many of the settings for the block editor including the following.

  • What customization options should be made available or hidden from the user.
  • What are the default colors, font sizes… available to the user.
  • Defines the default layout of the editor (widths and available alignments).

How theme.json works in the child starter theme.

The theme.json file in the child-starter theme is actually compiled from several mjs files located in .../src/theme-json/. In order to affect changes in theme.json you’ll need to edit the following files.

  • config.mjs
  • settings.mjs
  • styles.mjs

config.mjs

config.mjs houses core specifications for the theme.json file such as the theme.json version, as well as Custom Templates and Template part. It should look similar to the following in the child starter theme.

export default {
	"version": 1,
	"customTemplates": [ ],
	"templateParts": [ ]
}

“version”:

The version number sets what Theme Support features are available to use and the format. For WP5.8 this should always be set to 1, since version 2 was introduced in WP5.9, and version 3 in WP6.6.

For refnrece to the available propperties and and schema for version one visit https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-v1/

“customTemplates”: [ ]

This allows the declaration of custom templates present in the templates folder. For example, for a custom template named my-custom-template.html, the theme.json can declare what post types can use it and what’s the title to show the user

(not supported in WP5.8)

“templateParts”: [ ]

Whin this field themes can list the template parts present in the parts folder. For example, for a template part named my-template-part.html, the theme.json can declare the area term for the template part entity which is responsible for rendering the corresponding block variation (Header block, Footer block, etc.) in the editor.

(not supported in WP5.8)


settings.mjs

Settings covers a broad set of editor options and specifications that can be enabled/disabled or modified for a given theme. The following are available in version 1 (WP5.8).

border

Settings related to borders.

PropertyTypeDefaultProps
customRadiusbooleanfalse

color

Settings related to colors.

PropertyTypeDefaultProps
custombooleantrue
customDuotonebooleantrue
customGradientbooleantrue
duotonearraycolors, name, slug
gradientsarraygradient, name, slug
linkbooleanfalse
palettearraycolor, name, slug

layout

Settings related to layout.

PropertyTypeDefaultProps
contentSizestring
wideSizestring

spacing

Settings related to spacing.

PropertyTypeDefaultProps
customMarginbooleanfalse
customPaddingbooleanfalse
unitsarraypx,em,rem,vh,vw,%

typography

Settings related to typography.

PropertyTypeDefaultProps
customFontSizebooleantrue
customLineHeightbooleanfalse
dropCapbooleantrue
fontSizesarrayname, size, slug

custom

Generate custom CSS custom properties of the form --wp--custom--{key}--{nested-key}: {value};camelCased keys are transformed to kebab-case as to follow the CSS property naming schema. Keys at different depth levels are separated by --, so keys should not include -- in the name.


style.mjs

This file allows you to set style properties that are used globally for all blocks in the editor (top-level styles), as well as for specific blocks and block elements.

border

Border styles.

PropertyTypeProps
radiusstring

color

Color styles.

PropertyTypeProps
backgroundstring
gradientstring
textstring

spacing

Spacing styles.

PropertyTypeProps
marginobjectbottom, left, right, top
paddingobjectbottom, left, right, top

typography

Typography styles.

PropertyTypeProps
fontSizestring
lineHeightstring

More information can be found at https://developer.wordpress.org/block-editor/how-to-guides/themes/global-settings-and-styles/#styles

Using CSS Custom Properties

For many of these properties CSS Custom Properties (CSS Variables) can be used as opposed to fixed values. But it’s important to note that they must be used without spacing inside of the var() declaration, since this can cause errors in WordPresses style compilation process.

For example:
"wideSize": "var(--bu-container-max-inline-size--wide)" will work fine but "wideSize": "var( --bu-container-max-inline-size--wide )" will not.

Additionally If you’re using Custom Properties that are carried over from editor-styles.css or theme.css and defined in those stylesheets, the admin settings panels will not inherently know the values for those Custom properties.

For example if you’ve defined "--theme-color-bu: #C00" in editor-style.css and use "var(--theme-color-bu)" as a color in settings.mjs, the color will show up in the bounds of the block editor and the block itself but not necessarily in the swatch for the the blocks color settings panel. to correct for this you’ll want to either define -theme-color-bu: #C00" in the admin.css sheet or use the variable with a fallback like so "color": "var(--theme-color-bu,#c00)"