1

we have a collection of static blocks and we want to insert one of our statick blocks inside the PHP code of a different dynamic block but so far we didnt manage to do it:

Lets say the static block si called "create-block/claim-text"

<?php

    function html_our() {
    
      $html = '<div class="container">';

      /*this is where the static block "create-block/claim-text" goes*/

      $html .= '</div">';
    }

?>
3
  • can you be more specific what you mean by dynamic block? Do you mean server rendered? What do you mean by a static block? create-block/claim-text isn't a block I've seen and there are no examples, would it make sense to substitute it for a paragraph block? Server rendered blocks can't have child blocks, regardless of wether they're server rendered or not. html_our is also missing a huge amount of code and makes no sense as a function
    – Tom J Nowell
    Mar 10 at 14:21
  • The dynamic block is a server rendered gutenberg block that we made. create-block/claim-text is a regular JSX gutenberg block. It could be substituted for a paragraph block, but we dont even know how to do that. I didnt know server rendered blocks couldnt have child blocks, my bad. And yes, html_our doesnt make much sense, i just cut a lot of the original code just to make a quick example. Thank you.
    – JPD
    Mar 10 at 14:29
  • what does the dynamic block do? And are you sure it can't be replicated with a query block or block context/context providers?
    – Tom J Nowell
    Mar 10 at 14:34

1 Answer 1

0

Server rendered blocks like that can't have child blocks.

The closest you can do is call do_blocks and pass it block markup, but this means pre-creating the block in the editor, and copy pasting it out. It would not be modifiable in the editor, or appear as a block. It would just be additional HTML in your server rendered block.

If you want to get around that, you'll need to break your dynamic block up into multiple blocks. This is how site editing blocks work around this. For example they have query blocks that provide context that can be read by child blocks, then they get filtered on the frontend to replace individual blocks content. E.g. a post title block renders the post title in PHP using information it got from a parent. As a result you can insert any block you want into a query block, even though it's running PHP to grab data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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