admin管理员组文章数量:1289753
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?
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
Reset to default 1We 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 ) )
本文标签: wp enqueue scriptHow to target block variations with hasblock()
版权声明:本文标题:wp enqueue script - How to target block variations with has_block()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741415985a2377514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论