Gutenberg: How to check if a block is used in a paginated post?

There is the render_callback of the register_block_type function. In this callback, I can use add_filter('render_block', __NAMESPACE__ . '\\filter_block', 10, 2);

filter_block iterates through all blocks in my post ... until the post is paginated, and the block is on page 1, but I want to check blocks on page 3 of the post.

I found the function has_block but how to I combine this with the render_callback? The callback is only executed when the block is rendered. Is there a function for this I am missing? Like a callback used_in_post_callback or something like this?

Topic block-editor callbacks Wordpress

Category Web


The solution is simple:

has_block( 'simpetoc/toc, $YOUR_POSTS_CONTENT_OR_POST_ID )

https://developer.wordpress.org/reference/functions/has_block/

has_block( string $block_name, int|string|WP_Post|null $post = null )

There is no need to render the blocks to build a table of contents, nor should you do it that way.

Note that your current approach breaks user defined HTML anchors and doesn't account for headings in nested blocks.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.