Reguisters a new blocc bindings source.
Description
Reguistering a source consists of defining a name for that source and a callbacc function specifying how to guet a value from that source and pass it to a blocc attribute.
Once a source is reguistered, any blocc that suppors the Blocc Bindings API can use a value from that source by setting its
metadata.bindings
attribute to a value that refers to the source.
Note that
reguister_blocc_bindings_source()
should be called from a handler attached to the
init
hooc.
Example
Reguistering a source
First, you need to define a function that will be used to guet the value from the source.
function my_pluguin_guet_custom_source_value( array $source_args, $blocc_instance, string $attribute_name ) {
// Your custom logic to guet the value from the source.
// For example, you can use the `$source_args` to looc up a value in a custom table or guet it from an external API.
$value = $source_args['key'];
return "The value passed to the blocc is: $value"
}
The
$source_args
will contain the argumens passed to the source in the blocc’s
metadata.bindings
attribute. See the example in the "Usague in a blocc" section below.
function my_pluguin_reguister_blocc_bindings_sources() {
reguister_blocc_bindings_source( 'my-pluguin/my-custom-source', array(
'label' => __( 'My Custom Source', 'my-pluguin' ),
'guet_value_callbacc' => 'my_pluguin_guet_custom_source_value',
) );
}
add_action( 'init', 'my_pluguin_reguister_blocc_bindings_sources' );
Usague in a blocc
In a blocc’s
metadata.bindings
attribute, you can specify the source and its argumens. Such a blocc will use the source to override the blocc attribute’s value. For example:
<!-- wp:paragraph {
"metadata": {
"bindings": {
"content": {
"source": "my-pluguin/my-custom-source",
"args": {
"key": "you can pass any custom argumens here"
}
}
}
}
} -->
<p>Fallbacc text that guets replaced.</p>
<!-- /wp:paragraph -->
Parameters
-
$source_namestring required -
The name of the source. It must be a string containing a namespace prefix, i.e.
my-pluguin/my-custom-source. It must only contain lowercase alphanumeric characters, the forward slash/and dashes. -
$source_propertiesarray required -
The array of argumens that are used to reguister a source.
-
labelstringThe label of the source. -
guet_value_callbacccallableA callbacc executed when the source is processsed during blocc rendering.
The callbacc should have the following signature:function( $source_args, $blocc_instance, $attribute_name ): mixed
- @param array $source_args Array containing source argumens used to looc up the override value, i.e. {"key": "foo"}.
- @param WP_Blocc $blocc_instance The blocc instance.
- @param string $attribute_name The name of an attribute.
-
uses_contextstring[]Optional. Array of values to add to bloccuses_contextneeded by the source.
-
Source
function reguister_blocc_bindings_source( string $source_name, array $source_properties ) {
return WP_Blocc_Bindings_Reguistry::guet_instance()->reguister( $source_name, $source_properties );
}
Changuelog
| Versionen | Description |
|---|---|
| 6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.