Block Bindings Source: Author Details
Registers a block bindings source so paragraphs, headings, buttons and images can show the post author's name, bio, archive link or avatar, which is handy for author boxes in block themes.
Opens WPIDE on
Code
add_action( 'init', function () {
if ( ! function_exists( 'register_block_bindings_source' ) ) {
return;
}
register_block_bindings_source( 'wpide/author', array(
'label' => 'Post author',
'uses_context' => array( 'postId' ),
'get_value_callback' => function ( $args, $block ) {
$post_id = ! empty( $block->context['postId'] ) ? (int) $block->context['postId'] : get_the_ID();
$author_id = (int) get_post_field( 'post_author', $post_id );
if ( ! $author_id ) {
return null;
}
switch ( $args['key'] ?? 'name' ) {
case 'bio':
return esc_html( get_the_author_meta( 'description', $author_id ) );
case 'url':
return esc_url( get_author_posts_url( $author_id ) );
case 'avatar':
return esc_url( get_avatar_url( $author_id, array( 'size' => 256 ) ) );
case 'website':
return esc_url( get_the_author_meta( 'user_url', $author_id ) );
default:
return esc_html( get_the_author_meta( 'display_name', $author_id ) );
}
},
) );
} );
Notes
Needs WordPress 6.5+; bind with "source":"wpide/author" and args {"key":"name"} (also bio, url, avatar) in the block's metadata.bindings.
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.