0

I'm trying to create a dynamic Gutenberg block. In my block.json file I have specified render php file. The block doesn't have any attributes, it just does an apiFetch in the edit function to get taxonomies terms and list them in a ul, there is also no save function, since it just returns null. So how the get the created ul in my php? The documentation says that there are 3 variables exposed to the render file - $attributes, $content and $block, but when I do echo $content it returns an empty string.. Shouldn't that return my block content?

3
  • Are you trying to get the block content in the render file? That doesn't make much sense, because your render file is where you render the block content. Your render file should output the block content that you want. Dec 22, 2023 at 9:27
  • In my edit function in react I'm doing apiFetch and taking the terms of a custom taxonomy and I'm creating a ul. I need that ul that I created in the edit function.. I can get the terms and create the same ul in the PHP file, but what's the point of the edit function and the apiFetch then? I'm using a block based theme, so I can't just create a php page template and do all that there, instead I have to create a block.. Dec 22, 2023 at 11:00
  • Well the point of the edit function is to provide an editing interface and preview of the block in the editor. If your block does not need editing controls in the preview then just use a server side renderer, as documented for dynamic blocks: developer.wordpress.org/block-editor/how-to-guides/… Dec 22, 2023 at 11:49

1 Answer 1

-1

I'm just gonna pass the information generated from apiFetch as attributes to the PHP file, thus eliminating the need to do the whole get_terms() loop..

1
  • This means that any time the terms change the block won't be updated. Dec 22, 2023 at 11:47

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.