티스토리 수익 글 보기

티스토리 수익 글 보기

Docs: Document block_bindings_supported_attributes filter and pattern… · WordPress/gutenberg@518805a · GitHub
Skip to content

Commit 518805a

Browse files
committed
Docs: Document block_bindings_supported_attributes filter and pattern overrides in dynamic blocks
Adds comprehensive documentation for: – The block_bindings_supported_attributes filter for extending supported block attributes – The block_bindings_supported_attributes_{$block_type} dynamic filter with examples – How to access pattern override values in a dynamic block’s render_callback – Step-by-step guide with complete code examples for implementing pattern overrides in custom blocks
1 parent f037208 commit 518805a

File tree

1 file changed

+112
1
lines changed

1 file changed

+112
1
lines changed

docs/reference-guides/block-api/block-bindings.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ An example could be connecting an Image block `url` attribute to a function that
2323

2424
## Compatible blocks and their attributes
2525

26-
Right now, not all block attributes are compatible with block bindings. There is some ongoing effort to increase this compatibility, but for now, this is the list:
26+
Right now, not all block attributes are compatible with block bindings. There is some ongoing effort to increase this compatibility, but for now, this is the default list:
2727

2828
| Supported Blocks | Supported Attributes |
2929
| ---------------- | -------------------- |
@@ -32,6 +32,117 @@ Right now, not all block attributes are compatible with block bindings. There is
3232
| Image | id, url, title, alt |
3333
| Button | text, url, linkTarget, rel |
3434

35+
### Extending supported attributes
36+
37+
_**Note:** Since WordPress 6.9._
38+
39+
Developers can extend the list of supported attributes using the `block_bindings_supported_attributes` filter. This filter allows adding support for additional block attributes.
40+
41+
There are two filters available:
42+
43+
- `block_bindings_supported_attributes`: A general filter that receives the supported attributes array and the block type name.
44+
- `block_bindings_supported_attributes_{$block_type}`: A dynamic filter specific to a block type (e.g., `block_bindings_supported_attributes_core/image`).
45+
46+
Example of adding support for the `caption` attribute on the Image block:
47+
48+
```php
49+
add_filter(
50+
'block_bindings_supported_attributes_core/image',
51+
function ( $supported_attributes ) {
52+
$supported_attributes[] = 'caption';
53+
return $supported_attributes;
54+
}
55+
);
56+
```
57+
58+
Example of adding support for a custom block:
59+
60+
```php
61+
add_filter(
62+
'block_bindings_supported_attributes_my-plugin/my-block',
63+
function ( $supported_attributes ) {
64+
$supported_attributes[] = 'title';
65+
$supported_attributes[] = 'description';
66+
return $supported_attributes;
67+
}
68+
);
69+
```
70+
71+
This filter also affects which blocks and attributes are available for Pattern Overrides, as both features share the same underlying supported attributes configuration.
72+
73+
### Accessing Pattern Override values in dynamic blocks
74+
75+
When creating a dynamic block that supports Pattern Overrides, you can access the override values within your `render_callback` function. The Pattern block (`core/block`) provides override values to nested blocks via the `pattern/overrides` context.
76+
77+
**Step 1: Register your block with the required context and supported attributes**
78+
79+
```php
80+
add_action( 'init', function() {
81+
// Register supported attributes for pattern overrides
82+
add_filter(
83+
'block_bindings_supported_attributes_my-plugin/my-block',
84+
function ( $supported_attributes ) {
85+
$supported_attributes[] = 'title';
86+
$supported_attributes[] = 'description';
87+
return $supported_attributes;
88+
}
89+
);
90+
91+
register_block_type( 'my-plugin/my-block', array(
92+
'attributes' => array(
93+
'title' => array( 'type' => 'string', 'default' => '' ),
94+
'description' => array( 'type' => 'string', 'default' => '' ),
95+
'metadata' => array( 'type' => 'object' ),
96+
),
97+
// Declare that you need the pattern/overrides context
98+
'uses_context' => array( 'pattern/overrides' ),
99+
'render_callback' => 'my_block_render_callback',
100+
) );
101+
} );
102+
```
103+
104+
**Step 2: Access override values in your render callback**
105+
106+
The override values are stored in `$block->context['pattern/overrides']` as an associative array. The keys are block metadata names (assigned when enabling overrides), and the values are arrays of attribute overrides.
107+
108+
```php
109+
function my_block_render_callback( $attributes, $content, $block ) {
110+
// Get the block's metadata name (set when enabling overrides)
111+
$block_name = $attributes['metadata']['name'] ?? null;
112+
113+
// Get the pattern overrides from context
114+
$overrides = array();
115+
if ( $block_name && isset( $block->context['pattern/overrides'][ $block_name ] ) ) {
116+
$overrides = $block->context['pattern/overrides'][ $block_name ];
117+
}
118+
119+
// Get attribute values, preferring overrides when available
120+
// Note: An empty string in overrides means "reset to default"
121+
$title = $attributes['title'];
122+
if ( isset( $overrides['title'] ) && $overrides['title'] !== '' ) {
123+
$title = $overrides['title'];
124+
}
125+
126+
$description = $attributes['description'];
127+
if ( isset( $overrides['description'] ) && $overrides['description'] !== '' ) {
128+
$description = $overrides['description'];
129+
}
130+
131+
return sprintf(
132+
'<div class="my-block"><h3>%s</h3><p>%s</p></div>',
133+
esc_html( $title ),
134+
esc_html( $description )
135+
);
136+
}
137+
```
138+
139+
**Key points to keep in mind:**
140+
141+
- **`uses_context`**: Your block must declare `pattern/overrides` in its `uses_context` to receive override data from parent Pattern blocks.
142+
- **Block metadata name**: Each overridable block instance has a unique name stored in `$attributes['metadata']['name']`. This name is assigned when the user enables overrides on the block in the editor.
143+
- **Empty string convention**: An empty string (`""`) in the overrides represents a reset to the default value. Your code should handle this appropriately.
144+
- **Fallback behavior**: Always provide fallback values from `$attributes` in case the block is not inside a pattern or overrides are not set.
145+
35146
## Registering a custom source
36147

37148
Registering a source requires defining at least `name`, a `label` and a `callback` function that gets a value from the source and passes it back to a block attribute.

0 commit comments

Comments
 (0)