Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bacoords/block-settings
https://github.com/bacoords/block-settings
Last synced: 19 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bacoords/block-settings
- Owner: bacoords
- Created: 2024-06-13T17:57:22.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-26T17:59:26.000Z (5 months ago)
- Last Synced: 2024-10-09T09:20:54.659Z (about 1 month ago)
- Language: JavaScript
- Size: 193 KB
- Stars: 17
- Watchers: 6
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: readme.md
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.',
),
);
```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.
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?