Can I use Post Thumbnail with Block Bindings API

While the lightbox is not yet supported (as of WP version 6.5) for the featured image block, we can play with the block binding example from the docs by Justin Tadlock, on how to connect a custom block binding source to the core image block.

Let’s introduce a custom block binding source wpse/featured-image that provides the featured image url and alt text to the core image block.

The image block markup with our custom binding source settings becomes:

<!-- wp:image {
    "metadata":{
        "bindings":{
            "url":{
                "source":"wpse/featured-image",
                "args":{
                    "key":"url"
                }
            },
            "alt":{
                "source":"wpse/featured-image",
                "args":{
                    "key":"alt"
                }
            }
        }
    }
} -->
<figure class="wp-block-image">
    <img src="" alt=""/>
</figure>
<!-- /wp:image -->

where the custom binding source wpse/featured-image, that provides the url and alt values, is registered via PHP code:

add_action( 'init', 'wpse_register_block_bindings' );

function wpse_register_block_bindings() {
    register_block_bindings_source( 'wpse/featured-image', array(
        'label'              => esc_html__( 'Featured Image', 'wpse' ),
        'get_value_callback' => 'wpse_featured_image_bindings'
    ) );
}

function wpse_featured_image_bindings( $args ) {
    if ( ! isset( $args['key'] ) ) {
        return null;
    }

    if ( ! has_post_thumbnail() ) {
        return null;
    }

    $id  = get_post_thumbnail_id();
    $url = get_the_post_thumbnail_url();
    $alt = get_post_meta( $id, '_wp_attachment_image_alt', true );

    switch ( $args['key'] ) {
        case 'url':
            return esc_url( $url );
        case 'alt':
            return  esc_attr( $alt );
        default:
            return null;
    }
}

This is just a bare bone code example using the Block Binding API that hopefully you can adjust further to your needs! It might e.g. be better have a default image src and alt in the markup.

PS: Regarding your example, I’m not sure the bindings API supports a hidden registered meta field (with underscore in the key’s name), even though it’s registered meta that displays via REST API and also note that _thumbnail_id is just a number, not url.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino