Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if I have Reusable Block in post content? #17048

Closed
vicolaspetru opened this issue Aug 15, 2019 · 1 comment
Closed

How to check if I have Reusable Block in post content? #17048

vicolaspetru opened this issue Aug 15, 2019 · 1 comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@vicolaspetru
Copy link

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?

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Aug 16, 2019
@HardeepAsrani
Copy link
Contributor

You can do it like:

$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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

3 participants