You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use has_block function into this hook enqueue_block_assets to load specific scripts and/or styles depending on block in post_content. If I have the Reusable Block in the post, I can't check what kind of block it is, because the post_content is empty.
Can you suggest a method to enqueue styles and scripts only if the block are present in post? Or how can I check if post has reusable block?
The text was updated successfully, but these errors were encountered:
$content = get_post_field( 'post_content', $post_id );
$blocks = $this->parse_blocks( $content );
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
return;
}
foreach ( $blocks as $block ) {
if ( $block['blockName'] === 'core/block' && ! empty( $block['attrs']['ref'] ) ) {
// Here it will pass the reusable block ID, then you can use get the post content and get a list of all the blocks. Parse them and use it.
}
}
Let me know if it helps else I can write a custom has_reusable_block function that will also check for this.
Or maybe the Gutenberg team is also willing to have has_block function check for reusable_blocks, which should actually be the case?
I use
has_block
function into this hookenqueue_block_assets
to load specific scripts and/or styles depending on block inpost_content
. If I have the Reusable Block in the post, I can't check what kind of block it is, because thepost_content
is empty.Can you suggest a method to enqueue styles and scripts only if the block are present in post? Or how can I check if post has reusable block?
The text was updated successfully, but these errors were encountered: