Proof of concept of a register_block_setting()
function to quickly add settings fields to blocks with just PHP.
// Register a "Button Style" setting for the Button block.
wpdev_register_block_setting(
array(
'attribute' => 'prefixButtonStyle',
'blockTypes' => array( 'core/button' ),
'label' => 'Select Button Style',
'multiple' => false,
'options' => array(
array(
'value' => '',
'label' => 'Default',
),
array(
'value' => 'block-style-outline',
'label' => 'Outline',
),
array(
'value' => 'block-style-solid',
'label' => 'Solid',
),
),
'help' => 'Select a style for the button.',
),
);
The selected value will be added as a className to the block and saved as an attribute in the block's JSON data:
<div class="wp-block-button block-style-outline">
...
</div>
{
"prefixButtonStyle": "block-style-outline"
}
If you set multiple
to true, your options will be rendered as checkboxes instead of a select dropdown.
The selected values will be saved as an array in the block's JSON data:
{
"prefixButtonStyle": [ "block-style-outline", "block-style-solid" ]
}
This is also useful even if there's only one option but you want a checkbox/boolean behavior.
- optional render_block attribute to enable modifying the block markup in one palce
- some sort of conditional register_block_style support or inline CSS built in?