create-guten-block
create-guten-block copied to clipboard
Dynamic Blocks Support
Feature Request
Is your feature request related to a problem? Please describe. Gutenberg allows the use of the 'render_callback' in php when registering a block type. This isn't currently in use and makes it impossible to create dynamic blocks (like latest posts).
Describe the solution you'd like Implement a way of having a render_callback for each block.
Using a render_callback function is very welcome here as well! I can not figure out how dynamic blocks with the REST API work, the withSelect and @wordpress/data package are barely documented (like almost the entire Gutenberg repo..) so the PHP fallback is something that I could really use :)
I guess for now a solution is to make seperate plugins just for the blocks that need this. As long as CGB is only used for one block then the render_callback should work.
I can't find documentation on how to properly enqueue react to the save function, though I have seen references to it being possible.
Like here:
https://wordpress.stackexchange.com/questions/333532/how-to-add-javascript-function-in-the-save-of-gutenberg-block
https://www.youtube.com/watch?v=jauZCeLrGFA&t=1s
https://wp.zacgordon.com/2017/12/26/how-to-add-javascript-and-css-to-gutenberg-blocks-the-right-way-in-plugins-and-themes/
It would be great if this was included in create-guten-block, or at least added to documentation so it's clear on how to implement this. The php world is foreign to me, as I am more comfortable with react/javascript.
@Jebble I've started my own project to simplify WordPress development for myself. Not directly related to gutenberg block development, but it is also possible. Until this dynamic block support is added to cgb, you might want to check out my wp-plugin-boilerplate. You can simply add another entry file inside the wpds-scripts.config.js for your dynamic block and then enqueue the build script with a render_callback inside your plugin's PHP file.
This is not an advertisement, it's just to help those people who actually need to have access to the render callback, like myself 😉
I got a very simple solution that worked for me, you just need to add one more register_block_type
with the same name you registered in your js and just put the render_callback
option.
register_block_type(
'custom-block/your-block', array(
'render_callback' => 'block_render_callback'
)
);
function block_render_callback($attributes, $content) {
return 'Works!';
}
Did you try to do this with multiple blocks?
Did you try to do this with multiple blocks?
Yes, I have multiple blocks but just a few are dynamic.
Do you have a sample to show? I tried that but I still can't see the rendered code on the screen.
Did you put the same name in your javascript?
So... I just figure out the mistake that led to my previous question. Inside the plugin.php I can add the code that @rodrigowbazevedo showed before and, since the register_block_type has the same name as its javascript part, it works fine. My problem was a misconception about the way Create Guten Block works, I guess.
Did you try to do this with multiple blocks?
trying that now. It appears there is some extra support needed for render callback with multiple blocks. Going to investigate this now and well maybe I will figure it out and show an example.