-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass the block name to the render callback #4671
Comments
There are a number of "convert your shortcode to a block" tutorials that use the shortcode callback as the
So there's three easy solutions:
|
You can pass block name via attributes using filter in php's render_block function.
You can then access it in the render callback like this: |
A possible solution to this is being explored at #21467 (comment) , where These would both work, and effectively behave the same: register_block_type( 'my-plugin/favorite-color', [
'render_callback' => function( $block_attributes ) {
return 'Favorite color is: ' . esc_html( $block_attributes['color'] );
}
] ); register_block_type( 'my-plugin/favorite-color', [
'render_callback' => function( $block ) {
return 'Favorite color is: ' . esc_html( $block->attributes['color'] );
}
] ); It's still an early iteration, but feedback is welcome. |
This is now possible as of the changes merged in #21467. See documentation: https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/tutorials/block-tutorial/creating-dynamic-blocks.md Example: register_block_type( 'my-plugin/favorite-color', [
'render_callback' => function( $block ) {
return 'Block name: ' . esc_html( $block->name );
}
] ); |
is there a current method now that the render_callback only receives an attributes array? $block->name only throws an error. |
The above documentation about creating dynamic blocks has moved to https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/block-tutorial/creating-dynamic-blocks.md |
Thank you, I had found the new location earlier... but the problem is the render_callback no longer appears to receive a WP_Block instance which was relevant to resolving this issue. |
Issue Overview
The block name is not passed as a parameter to the
render_callback
callback function. This means it's not possible to use a generic callback which performs routing to render dynamic blocks (for example, a generic handler which routes block rendering to theme template parts).Expected Behavior
The block name is passed as a parameter to the
render_callback
callback function.Current Behavior
Just the block attributes and content are passed.
The text was updated successfully, but these errors were encountered: