Bind Blocks to Site Details (Block Bindings)
Registers a Block Bindings source so Paragraph, Heading and Button blocks can display the site name, tagline, URL or current year and stay up to date automatically.
Opens WPIDE on
Code
add_action( 'init', function () {
if ( ! function_exists( 'register_block_bindings_source' ) ) {
return;
}
register_block_bindings_source( 'wpide/site-details', array(
'label' => 'Site details',
'get_value_callback' => function ( $source_args ) {
$key = isset( $source_args['key'] ) ? $source_args['key'] : '';
switch ( $key ) {
case 'name':
return esc_html( get_bloginfo( 'name' ) );
case 'tagline':
return esc_html( get_bloginfo( 'description' ) );
case 'url':
return esc_url( home_url( '/' ) );
case 'year':
return esc_html( wp_date( 'Y' ) );
}
return null;
},
) );
} );
Notes
Requires WordPress 6.5 or newer. In the code editor use for example {"metadata":{"bindings":{"content":{"source":"wpide/site-details","args":{"key":"year"}}}}} on a Paragraph block. Keys are name, tagline, url and 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.