1

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?

7
  • 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 1

1

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.

3
  • thanks. As mentioned in the original question I already found that function. I will try to use that somehow. I need to alter the content and the blocks within because the headings need an id to jump to. But thank you very much.
    – Marc
    Commented Mar 14, 2022 at 17:32
  • Thanks. has_block and parse_blocks was the solution in the end: github.com/mtoensing/simpletoc/commit/…
    – Marc
    Commented Mar 14, 2022 at 18:14
  • yes but you were unaware of the second parameter, and it seems you haven't realised its importance. I've left some notes on the commit you linked to
    – Tom J Nowell
    Commented Mar 14, 2022 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.