Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bacoords/block-settings


https://github.com/bacoords/block-settings

Last synced: 19 days ago
JSON representation

Awesome Lists containing this project

README

        

# Block Settings

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

## Usage

```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.',
),
);
```
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:

```html


...

```

```json
{
"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:

```json
{
"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?