Several core Core is the set of software required to run WordPress. The Core Development Team builds WordPress. and third party blocks share different customization tools. Things like color, background, font size, alignment and others are customizations options block Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. authors are likely to add to their own block. To increase the consistency and also make it easier to introduce these options into their blocks, block authors can use the Block supports API An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways.. WordPress 5.6 will see the introduction of a number of new block supports and allows dynamic blocks to use them as well.
Blocks can declare support for style properties
To enable the new block supports, add the desired keys to the “supports” property of the block.json
file or directly into the registerBlockType
function. The block editor will automatically show a UI User interface control for the user to set those values.
supports: {
color: {
background: true, // Enable background color UI control.
gradient: true, // Enable gradient color UI control.
text: true // Enable text color UI control.
},
fontSize: true, // Enable font size UI control.
lineHeight: true // Enable line height UI control.
}
For some of these properties, the active theme may need to opt-in for them to be enabled. That’s the case of lineHeight, for example.
How it works
When the user interacts with the controls, they will automatically attach the style value to the wrapper element of the block. This works as it did before:
- If the control offers some preset values and the user selects one of them, a class will be attached with the name
.has-<value>-<preset-category>
(i.e.: has-normal-font-size
). - If the user selects a custom value, it’ll be attached as part of the
style
attribute of the wrapper.
Note that these are attached to the wrapper element of the block, so it works best with blocks that follow the newest apiVersion (see related dev note).
The UI controls that present users with a set of preset values, take them from the corresponding theme supports:
Dynamic blocks support
You can also use the block supports in dynamic blocks by making sure the config is added to the register_block_type
server side (or the block.json JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML.) and by rendering the block wrapper attributes in your block’s render callback function:
function my_block_render_callback_function( $attributes ) {
$wrapper_attributes = get_block_wrapper_attributes();
$content_markup = "something";
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$content_markup
);
}
In depth
When blocks declare support for a style property, they also gain additional block attributes. These can also be used to set default values:
attributes: {
backgroundColor: {
type: 'string',
default: 'value',
},
gradient: {
type: 'string',
default: 'value',
},
textColor: {
type: 'string',
default: 'value',
},
fontSize: {
type: 'string',
default: 'value'
},
style: {
type: 'Object',
default: {
color: {
background: 'value',
gradient: 'value',
link: 'value',
text: 'value'
},
typography: {
fontSize: 'value',
lineHeight: 'value',
}
}
}
}
The attributes backgroundColor
, gradient
, textColor
, and fontSize
hold any preset values the user has selected. When the user sets a custom value instead, these are stored in the corresponding key within the style
attribute. Check out the documentation for more details.
#5-6, #block-editor, #dev-notes