Skip to content

bacoords/block-settings

Repository files navigation

Block Settings

Proof of concept of a register_block_setting() function to quickly add settings fields to blocks with just PHP.

Usage

	// 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.',
		),
	);
Xnapper-2024-06-13-15 23 31

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"
}

Multiple values support

If you set multiple to true, your options will be rendered as checkboxes instead of a select dropdown.

Xnapper-2024-06-13-15 40 02

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.

Potential next steps...

  • 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?

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published