bacoords / block-settings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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


Languages

Language:JavaScript 42.2%Language:PHP 41.9%Language:CSS 11.0%Language:SCSS 4.9%