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?
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?
- what's the problem you're trying to solve by doing this? What's the context? It's possible to get an answer that answers this question but is completely unusable for solving your problem if you don't share, or solves it in a very complicated and limiting way – Tom J Nowell ♦ Commented Mar 14, 2022 at 15:28
- It is about my open-source SimpleTOC plugin. If the user places the block on the first page of the paginated post I can't use "render_callback" because the block is not rendered. But I need to manipulate the headings in the content. – Marc Commented Mar 14, 2022 at 15:49
- I tried this: 'add_filter( 'the_content', 'checkforblock', 1 ); function checkforblock( $content ) { if ( has_block( 'simpletoc/toc' ) ) { // manipulate content } return $content; }' But how do I iterate through all the blocks in "has_block". The content outputs nothing I can do a foreach to go through each block. – Marc Commented Mar 14, 2022 at 16:22
- I'm not sure I understand, I'm missing some assumed steps or pieces of knowledge, why would a table of contents block need to manipulate headings, and why would it need to manipulate the HTML of a block that isn't being rendered? And why do you need to render the block for the table of contents to work? A table of contents should work with just the raw post content or even just a post ID – Tom J Nowell ♦ Commented Mar 14, 2022 at 16:23
- "Why would a table of contents block need to manipulate headings, and why would it need to manipulate the HTML of a block that isn't being rendered? " Because the headings need an id to jump to. And the toc is on page 1 and the headings are on another page – Marc Commented Mar 14, 2022 at 19:54
1 Answer
Reset to default 1The 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.