我们可以尝试使用has_block()
, has_blocks()
和parse_blocks()
查找具有给定providerNameSlug
属性
未经测试的建议:
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 ) )