Block Bindings Source: Formatted Post Dates
Registers a block bindings source that fills blocks with the post's published or last updated date in any format, so you can write "Updated on ..." in a paragraph inside block templates.
Opens WPIDE on
Code
add_action( 'init', function () {
if ( ! function_exists( 'register_block_bindings_source' ) ) {
return;
}
register_block_bindings_source( 'wpide/dates', array(
'label' => 'Post dates',
'uses_context' => array( 'postId' ),
'get_value_callback' => function ( $args, $block ) {
$post = get_post( ! empty( $block->context['postId'] ) ? (int) $block->context['postId'] : get_the_ID() );
if ( ! $post ) {
return null;
}
$format = isset( $args['format'] ) ? sanitize_text_field( $args['format'] ) : get_option( 'date_format' );
switch ( $args['key'] ?? 'published' ) {
case 'modified':
return esc_html( get_the_modified_date( $format, $post ) );
case 'year':
return esc_html( get_the_date( 'Y', $post ) );
default:
return esc_html( get_the_date( $format, $post ) );
}
},
) );
} );
Notes
Needs WordPress 6.5+; bind with "source":"wpide/dates" and args {"key":"modified","format":"F j, Y"} (keys published, modified, year).
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.