The Block Bindings API: show custom fields in core blocks
Before the Block Bindings API, showing a custom field in a block theme meant a custom block or a shortcode. With bindings (WordPress 6.5), a core block attribute, such as a paragraph’s content, an image’s URL or a button’s link, can be connected to a data source. WordPress ships the core/post-meta source for custom fields, and plugins can register their own sources. Since 6.7, editors can create the connections in the block sidebar.
Problem: A real estate site stores price, rooms and the floor plan image as post meta of the ha_property post type. The single template is a block theme template, and the values are currently inserted with a shortcode block that editors keep breaking.
Solution: Register the meta fields with show_in_rest and a label, bind Paragraph and Image blocks to them with the core/post-meta source in the template markup, and register a custom source with register_block_bindings_source() for computed values such as a formatted price.
Register the fields
add_action( 'init', function (): void {
foreach ( array(
'ha_rooms' => array( 'type' => 'string', 'label' => __( 'Rooms', 'textdomain' ) ),
'ha_floor_plan' => array( 'type' => 'string', 'label' => __( 'Floor plan URL', 'textdomain' ) ),
'ha_price' => array( 'type' => 'number', 'label' => __( 'Price', 'textdomain' ) ),
) as $key => $args ) {
register_post_meta( 'ha_property', $key, array(
'type' => $args['type'],
'label' => $args['label'],
'single' => true,
'show_in_rest' => true,
'auth_callback' => fn() => current_user_can( 'edit_posts' ),
) );
}
} );
Only meta registered with show_in_rest can be bound; the label is what the editor shows in the bindings panel. Meta keys starting with an underscore are protected and not available for binding.
Bind blocks in a template
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"ha_rooms"}}}}} -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:image {"metadata":{"bindings":{"url":{"source":"core/post-meta","args":{"key":"ha_floor_plan"}}}}} -->
<figure class="wp-block-image"><img alt=""/></figure>
<!-- /wp:image -->
On the front end, WordPress replaces the bound attributes with the meta values of the current post. In the editor, the block shows the value and, for users who can edit the meta, allows editing it directly in the block. Bindable attributes are limited to supported blocks: Paragraph (content), Heading (content), Image (url, alt, title), Button (url, text, linkTarget, rel); the list grows with releases.
A custom source for computed values
add_action( 'init', function (): void {
register_block_bindings_source( 'ha/property', array(
'label' => __( 'Property data', 'textdomain' ),
'uses_context' => array( 'postId' ),
'get_value_callback' => function ( array $args, WP_Block $block ) {
$post_id = $block->context['postId'] ?? get_the_ID();
if ( 'price' === ( $args['field'] ?? '' ) ) {
$price = (float) get_post_meta( $post_id, 'ha_price', true );
return $price ? wp_strip_all_tags( wc_price( $price ) ) : __( 'Price on request', 'textdomain' );
}
return null;
},
) );
} );
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"ha/property","args":{"field":"price"}}}}} -->
<p></p>
<!-- /wp:paragraph -->
The callback returns the value for the bound attribute; uses_context gives access to the post ID inside a Query Loop, so the same binding works in archive cards. The wc_price() call assumes WooCommerce for currency formatting; replace it with number_format_i18n() without it. The returned value is escaped by the block’s rendering, but returning plain text (no HTML) is the safe pattern.
NOTE: Bindings render on the server, so the values are in the HTML for visitors and search engines. For values that need logic in the editor too (live preview of a formatted price), a source can also be registered in JavaScript with registerBlockBindingsSource() from @wordpress/blocks. Overview of the release that added the editor UI for bindings: WordPress 6.7 for developers.