create-guten-block icon indicating copy to clipboard operation
create-guten-block copied to clipboard

Dynamic Blocks Support

Open josias-r opened this issue 5 years ago • 10 comments

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.

josias-r avatar Jun 12 '19 12:06 josias-r

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.

Jebble avatar Jun 21 '19 11:06 Jebble

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.

noahneumark avatar Sep 03 '19 00:09 noahneumark

@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 😉

josias-r avatar Sep 22 '19 13:09 josias-r

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!';
}

rodrigowbazevedo avatar Nov 23 '19 21:11 rodrigowbazevedo

Did you try to do this with multiple blocks?

olafghanizadeh avatar Nov 24 '19 10:11 olafghanizadeh

Did you try to do this with multiple blocks?

Yes, I have multiple blocks but just a few are dynamic.

rodrigowbazevedo avatar Nov 24 '19 15:11 rodrigowbazevedo

Do you have a sample to show? I tried that but I still can't see the rendered code on the screen.

rhamses avatar Mar 19 '20 21:03 rhamses

Did you put the same name in your javascript?

rodrigowbazevedo avatar Mar 19 '20 21:03 rodrigowbazevedo

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.

rhamses avatar Jun 09 '20 12:06 rhamses

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.

megphillips91 avatar Jul 03 '20 15:07 megphillips91