Populate dynamically blocks (button, image, paragraph and heading), by setting the source, from meta field or php logic.
meta field
<!-- wp:paragraph {
"metadata":{
"bindings":{
"content":{
"source":"core/post-meta",
"args":{
"key":"book-genre"
}
}
}
}
} -->
<p></p>
<!-- /wp:paragraph -->
register_meta(
'post',
'book-genre',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'Default text field',
)
);
php logic
$source_name = 'namespace/name';
$source_properties = array(
'label' => 'Name',
'get_value_callback' => 'fn',
'uses_context' => ['postId']
);
register_block_bindings_source(
string $source_name,
array $source_properties
);
projectslug_bindings_callback(
array $source_args,
WP_Block $block_instance,
string $attribute_name
);
Source: https://make.wordpress.org/core/2024/03/06/new-feature-the-block-bindings-api/