最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

Share Improve this question edited Mar 14, 2022 at 15:15 kero 6,3201 gold badge25 silver badges34 bronze badges asked Mar 14, 2022 at 15:03 MarcMarc 6979 silver badges28 bronze badges 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
 |  Show 2 more comments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论