1

We often use has_block to enqueue scripts only if a certain block is present. With something like vimeo you used to be able to write has_block('core-embed/vimeo').

What is the proper way now to enqueue scripts for JUST the vimeo variation, not all core/embed 's?

1 Answer 1

1

We can try to use has_block(), has_blocks() and parse_blocks() to find an embed with a given providerNameSlug attribute.

Untested suggestion:

function has_block_embed_by_provider_wpse( $provider, $post = null ) {
    if ( ! has_blocks( $post ) ) {
        return false;
    }
 
    if ( ! is_string( $post ) ) {
        $wp_post = get_post( $post );
        if ( $wp_post instanceof WP_Post ) {
            $post = $wp_post->post_content;
        }
    }
   
    if ( has_block ( 'embed', $post ) ) {
        $blocks = parse_blocks ( $post );
        foreach( (array) $blocks as $block ) {
            if ( isset( $block['attrs']['providerNameSlug'] ) 
                 && $provider === $block['attrs']['providerNameSlug'] 
            ) {
                return true;
            }
        }
    }
    return false;
}

Usage Examples:

has_block_embed_by_provider_wpse( 'vimeo' )

has_block_embed_by_provider_wpse( 'vimeo', $content )

has_block_embed_by_provider_wpse( 'vimeo', $post )

has_block_embed_by_provider_wpse( 'vimeo', 123 )

has_block_embed_by_provider_wpse( 'vimeo', get_post( 123 ) )
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.