You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function register_dynamic_block() {
// Only load if Gutenberg is available.
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
// Hook server side rendering into render callback
register_block_type( 'matt-watson/secure-block', [
'render_callback' => 'matt_watson\secure_blocks_for_gutenberg\matt_watson_secure_blocks_for_gutenberg_render',
] );
}
Then you can grab the content in this function:
function matt_watson_secure_blocks_for_gutenberg_render( $attributes, $content ) {
return $content;
}
Note that I use a namespace in the plugin which is needed when calling the function.
Another example, in one of my blocks I have multiple InnerBlocks areas and want to save them all:
el( wp.editor.RichText, {
tagName: 'div',
format: 'string',
placeholder: 'Some text',
value: someAttribute,
onChange: function( content ) {
props.setAttributes({ someAttribute: content }); // I can save this in "someAttribute" easily...
}
} ),
el(wp.editor.InnerBlocks), // ...but how to save this? And then access and render?
el(wp.editor.InnerBlocks), // ...and this?
el(wp.editor.InnerBlocks), // ...and this?
@rameezwp We implemented a small polyfill for that problem: Gutenberg hooks into "the_content" to parse the Block-Comments. We also hooked in with preference 0 so that our filter starts first. Then we have a look into the Gutenberg Block Registry, look for our tags with inner content and called the registered callback ourself. Since Gutenberg Block Meta Comments are very structured, the needed regular expressions to identify tags and inner content were found very easely.
We did that only for nested Gutenberg Blocks and left single tags to the Gutenberg filter.
The solution has the benefit that you can work as documented by gutenberg and just turn off your filter as soon as the Gutenberg team has solved that issue.
@dmsnell thank you for the reference. I can see where your solution will lead but wouldn't it be easier to first render the sub HTML content and then passing it's content to the upper levels?
Example:
World
-> House
--> Table
With your solution if you render World you would have to manually render the house and then make a recursive function to render table as in table could be another block an so on. What if the InnerBlocks had an onChange function which would return the content. This content is allready rendered and doesen't has to be checked for not rendered content as it is already done by it's predecessor (for example table).
@NoelElias in #10108 I'm exploring a filtering API but you can see there that the inner blocks are already accessible to parent blocks in PHP as rendered HTML.
I think there are two distinct ways we might want to process a block in PHP: before it's turned into HTML - "structural filtering"; after it's turned into HTML. For example, maybe I want to filter out any inner blocks of the type conditional/on-sunday - then I want access to the structure and not just the rendered HTML. On the other hand, maybe I'm concatenating my inner blocks with the normal output but one of them is dynamic - I need to make sure that render/filter gets in before I use the serialized HTML values. Does that make any sense?
Anyway, I'd love to have you share your input in #10108, or at least the PHP-specific bits. There are some trivial things we can do like pass the array of rendered inner blocks to the render_callback but that trivial action also carries non-trivial implications.
@dmsnell I think this solution works for editing the innerBlock although registering a callback function inside register_block_type would do the job as well. I personally don't like filter functions but that's just my opinion.
This feature will be important but I still think, that having access to the rendered inner blocks inside render_callback is so much more important right now. Without it people will, for example, start hard coding their headings inside their blocks instead using the heading block provided.
Also with your solution, passing the rendered inner blocks to the render_callback won't be a problem anymore as you can edit them with the filter function before they get passed to the render_callback.
If I am following correctly, the update in https://core.trac.wordpress.org/ticket/45451 to add a filter to override block attributes solves the problems raised here (and noting that update should ship with WordPress 5.1). @manake, if that is not correct, would you mind please leaving me a comment to let me know?
Regardless of this perhaps being an old thread, it still came up high in google for a search I performed to find out how to output InnerBlocks in PHP, and there isn't much else in the way of info on how to do this. So I wanted to share how I got it working...
In your php file for the block, you will have a function register_block_type, and in there is your render_callback. Whatever function your sending your render_callback to, give that function two parameters - $attributes and $content. Your $content should be your InnerBlocks rendering out.
function your_render_callback($attributes, $content) {
echo $content; // this should be your InnerBlock output.
}
how do I modify block.js (I assume this is where my problem is). I have tried various combinations of ServerSideRender and save : ... return null;
but I can't put it all together. In the block editor I see "block renders empty or undefined and I can't drag anything into it.
You can return null in the save function because your block will be rendered server-side (using PHP). However, when you're using InnerBlocks, the InnerBlocks.Content has to be returned, otherwise it will not be accessible to the PHP rendering function.
Then, in the rendering function, you must use the following:
How can I access the attributes of a block within InnerBlocks? Let's say contains a core/gallery block inside which I want to render on my own. How can I get the gallery ID or the Image IDs in the server-side render.php?
If you can't find any reference to your parent block, you could maybe get the current post content, use parse_blocks to get the whole block structure as an nested array, search for your block id and go from there ...
I tried accessing the content via the $content variable and via get_the_content().
In both cases I don't see the attributes.
This is my callback function:
function render_html($attributes, $content) {
ob_start(); ?>
<h1>Attributes</h1>
<h3>The number of columns is <?php echo esc_html($attributes['myColumns']) ?>!</h3>
<p>The content is <?php echo esc_html($content) ?>!</p>
<p>The content is <?php echo esc_html(get_the_content()) ?>!</p>
<?php
return ob_get_clean();
}
This is was the output
Attributes
The number of columns is 3! The content is <div class="wp-block-mywebsitename-generic-cards has-3-columns"> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Robert-3.jpg" alt="" class="wp-image-10496"/><h4>Robert</h4><p>Bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="http::/facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li></ul></div></div> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Phil-3.jpg" alt="" class="wp-image-10499"/><h4>Phil</h4><p>Phil's bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="https://facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li><li data-icon="twitter"><a href="https://twitter.com"><span class="dashicon dashicons dashicons-twitter"></span></a></li></ul></div></div> <div class="wp-block-blocks-course-team-member"></div> </div> !
This would be the easier way to access the innerblocks data in the block PHP render:
<?php
/**
* Render Custom Block
*
* @param array $atts Block Attributes
* @param string $content Block Content (InnerBlocks)
* @param WP_Block $block_class Block Class Instance
* @return string Block Markup
*/
function render( $atts, $content, $block_class ) {
// Check how many inner blocks are available
$item_count = $block_class->inner_blocks->count();
if ( $item_count < 1 ) {
return '';
}
ob_start();
?>
<div>
<?php
// iterate over the available inner blocks
for ( $index = 1; $index <= $item_count; $index++ ) :
// Get the inner block data
$inner_block = $block_class->inner_blocks->current();
// Holds the inner block attribute
$attribute = $inner_block->attributes;
// This will display the attributes data
var_dump( $attribute );
// increase the index in the WP_Block_List class used to retrieve the current block
$block_class->inner_blocks->next();
endfor;
// reset the index in the WP_Block_List class to the initial state
$block_class->inner_blocks->rewind();
?>
</div>
<?php
return ob_get_clean();
}
?>
Activity
mattwatsoncodes commentedon Aug 12, 2018
Hi @manake,
This file in one of my plugins may help you:
https://github.com/mwtsn/secure-blocks-for-gutenberg/blob/master/blocks/secure-block/php/class-secure-block.php
In particular these two blocks of code:
Then you can grab the content in this function:
Note that I use a namespace in the plugin which is needed when calling the function.
georgestephanis commentedon Oct 10, 2018
@mwtsn The issue is that the InnerBlocks aren't currently passed to the parent block's content -- or rendered even, it seems.
Related: #6751
This is a known issue, I talked to both @aduth and @dmsnell about it last week -- I'm just popping around and documenting on open issues right now.
manake commentedon Oct 30, 2018
@mattwatsoncodes
$contentis empty for me.Another example, in one of my blocks I have multiple
InnerBlocksareas and want to save them all:rameezwp commentedon Nov 2, 2018
Any workaround for accessing InnerBlocks.Content in php yet?
jjarolim commentedon Nov 2, 2018
@rameezwp We implemented a small polyfill for that problem: Gutenberg hooks into "the_content" to parse the Block-Comments. We also hooked in with preference 0 so that our filter starts first. Then we have a look into the Gutenberg Block Registry, look for our tags with inner content and called the registered callback ourself. Since Gutenberg Block Meta Comments are very structured, the needed regular expressions to identify tags and inner content were found very easely.
We did that only for nested Gutenberg Blocks and left single tags to the Gutenberg filter.
The solution has the benefit that you can work as documented by gutenberg and just turn off your filter as soon as the Gutenberg team has solved that issue.
BR from Salzburg!
rameezwp commentedon Nov 2, 2018
So how I can actually render innerblocks from php callback?
block.js
save: function(props) { return el( InnerBlocks.Content ); },php file
function render_block($attrs, $content){ ob_start(); echo $content; return ob_get_clean(); }Thanks for your help.
Parser: Add new list of HTML fragments to parse output (#11334)
noel-schenk commentedon Nov 19, 2018
@dmsnell thank you for the reference. I can see where your solution will lead but wouldn't it be easier to first render the sub HTML content and then passing it's content to the upper levels?
Example:
World
-> House
--> Table
With your solution if you render World you would have to manually render the house and then make a recursive function to render table as in table could be another block an so on. What if the InnerBlocks had an onChange function which would return the content. This content is allready rendered and doesen't has to be checked for not rendered content as it is already done by it's predecessor (for example table).
So your js code for World could look like this:
@chrisvanpatten and @jorgefilipecosta #7785 #11271 #10745 #6751
dmsnell commentedon Nov 19, 2018
@NoelElias in #10108 I'm exploring a filtering API but you can see there that the inner blocks are already accessible to parent blocks in PHP as rendered HTML.
I think there are two distinct ways we might want to process a block in PHP: before it's turned into HTML - "structural filtering"; after it's turned into HTML. For example, maybe I want to filter out any inner blocks of the type
conditional/on-sunday- then I want access to the structure and not just the rendered HTML. On the other hand, maybe I'm concatenating my inner blocks with the normal output but one of them is dynamic - I need to make sure that render/filter gets in before I use the serialized HTML values. Does that make any sense?Anyway, I'd love to have you share your input in #10108, or at least the PHP-specific bits. There are some trivial things we can do like pass the array of rendered inner blocks to the
render_callbackbut that trivial action also carries non-trivial implications.noel-schenk commentedon Nov 22, 2018
@dmsnell I think this solution works for editing the innerBlock although registering a callback function inside
register_block_typewould do the job as well. I personally don't like filter functions but that's just my opinion.This feature will be important but I still think, that having access to the rendered inner blocks inside render_callback is so much more important right now. Without it people will, for example, start hard coding their headings inside their blocks instead using the heading block provided.
Also with your solution, passing the rendered inner blocks to the
render_callbackwon't be a problem anymore as you can edit them with the filter function before they get passed to therender_callback.designsimply commentedon Jan 25, 2019
If I am following correctly, the update in https://core.trac.wordpress.org/ticket/45451 to add a filter to override block attributes solves the problems raised here (and noting that update should ship with WordPress 5.1). @manake, if that is not correct, would you mind please leaving me a comment to let me know?
8 remaining items
rodrigodagostino commentedon Jul 5, 2019
@aduth yes, I understand those are the arguments I need to supply, but where am I supposed to get those from inside my render function?
aduth commentedon Jul 5, 2019
The inner blocks are not currently exposed on
render_callback. I don't recall if this was intentional.Instead, you can use the above filter and test whether
$block['blockName']is the name of the block you've implemented.rodrigodagostino commentedon Jul 8, 2019
Thanks for your efforts, @aduth , I still do not really understand how to use that filter. I might take a look to it later.
Thanks for your time too.
flywolfcreative commentedon Mar 9, 2020
Regardless of this perhaps being an old thread, it still came up high in google for a search I performed to find out how to output InnerBlocks in PHP, and there isn't much else in the way of info on how to do this. So I wanted to share how I got it working...
In your php file for the block, you will have a function
register_block_type, and in there is yourrender_callback. Whatever function your sending your render_callback to, give that function two parameters -$attributesand$content. Your$contentshould be your InnerBlocks rendering out.Hope this helps anyone who is slightly lost.
johnnya23 commentedon Mar 16, 2020
@flywolfcreative or anyone else:
php:
register_block_type('jma-ghb/featued-block', array( 'attributes' => array( ), 'editor_script' => 'jma-ghb-featured-script', 'render_callback' => 'JMA_GHB_featured_callback', ));and:
function JMA_GHB_featured_callback($x, $y) { ob_start(); echo $y; return ob_get_clean(); }wordpress gives us this for block.js https://developer.wordpress.org/block-editor/tutorials/block-tutorial/nested-blocks-inner-blocks/:
( function( blocks, element, blockEditor ) { var el = element.createElement; var InnerBlocks = blockEditor.InnerBlocks; blocks.registerBlockType( 'jma-ghb/featued-block', { title: 'Example: Inner Blocks', category: 'layout', edit: function( props ) { return el( 'div', { className: props.className }, el( InnerBlocks ) ); }, save: function( props ) { return el( 'div', { className: props.className }, el( InnerBlocks.Content ) ); }, } ); } ( window.wp.blocks, window.wp.element, window.wp.blockEditor, ) );how do I modify block.js (I assume this is where my problem is). I have tried various combinations of ServerSideRender and save : ... return null;
but I can't put it all together. In the block editor I see "block renders empty or undefined and I can't drag anything into it.
thanks
ETVO commentedon May 21, 2020
@flywolfcreative
And what do you write in JS? Does your save function return null or
InnerBlocks.Content?ETVO commentedon Aug 9, 2021
@debojyotighosh this is what works for me to use InnerBlocks content and render it using PHP (server-side)
In your registerBlockType JS (focus on the save property):
You can return null in the save function because your block will be rendered server-side (using PHP). However, when you're using InnerBlocks, the InnerBlocks.Content has to be returned, otherwise it will not be accessible to the PHP rendering function.
Then, in the rendering function, you must use the following:
vladoa commentedon Nov 14, 2021
How can I access the attributes of a block within InnerBlocks? Let's say contains a
core/galleryblock inside which I want to render on my own. How can I get the gallery ID or the Image IDs in the server-side render.php?StephenBugden commentedon Apr 28, 2022
Hello, Does anyone have an answer to the last question from @vladoa ?
I'm trying to access the attributes from the innerblocks and can't find a way to do it.
jjarolim commentedon Apr 28, 2022
If you can't find any reference to your parent block, you could maybe get the current post content, use parse_blocks to get the whole block structure as an nested array, search for your block id and go from there ...
StephenBugden commentedon Apr 28, 2022
Hi @jjarolim,
Thanks for the idea, I'll take a look.
Assume one of the fields in the post content will contain the attributes.
StephenBugden commentedon Apr 28, 2022
Hi @jjarolim,
I tried accessing the content via the $content variable and via get_the_content().
In both cases I don't see the attributes.
This is my callback function:
This is was the output
Attributes
The number of columns is 3!
The content is <div class="wp-block-mywebsitename-generic-cards has-3-columns"> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Robert-3.jpg" alt="" class="wp-image-10496"/><h4>Robert</h4><p>Bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="http::/facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li></ul></div></div> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Phil-3.jpg" alt="" class="wp-image-10499"/><h4>Phil</h4><p>Phil's bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="https://facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li><li data-icon="twitter"><a href="https://twitter.com"><span class="dashicon dashicons dashicons-twitter"></span></a></li></ul></div></div> <div class="wp-block-blocks-course-team-member"></div> </div> !The content is <!– wp:block {"ref":4939} /–> <!– wp:block {"ref":6275} /–> <!– wp:kadence/rowlayout {"uniqueID":"_26eee5-27","columns":1,"colLayout":"equal","inheritMaxWidth":true} –> <div class="wp-block-kadence-rowlayout alignnone"><div id="kt-layout-id_26eee5-27" class="kt-row-layout-inner kt-layout-id_26eee5-27"><div class="kt-row-column-wrap kt-has-1-columns kt-gutter-default kt-v-gutter-default kt-row-valign-top kt-row-layout-equal kt-tab-layout-inherit kt-m-colapse-left-to-right kt-mobile-layout-row kb-theme-content-width"><!– wp:kadence/column {"uniqueID":"_3ab532-7c"} –> <div class="wp-block-kadence-column inner-column-1 kadence-column_3ab532-7c"><div class="kt-inside-inner-col"><!– wp:mywebsitename/generic-cards –> <div class="wp-block-mywebsitename-generic-cards has-3-columns"><!– wp:blocks-course/team-member {"id":10496} –> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Robert-3.jpg" alt="" class="wp-image-10496"/><h4>Robert</h4><p>Bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="http::/facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li></ul></div></div> <!– /wp:blocks-course/team-member –> <!– wp:blocks-course/team-member {"id":10499} –> <div class="wp-block-blocks-course-team-member"><img src="https://mywebsitename6.local/wp-content/uploads/2022/04/Phil-3.jpg" alt="" class="wp-image-10499"/><h4>Phil</h4><p>Phil's bio</p><div class="wp-block-blocks-course-team-member-social-links"><ul><li data-icon="facebook"><a href="https://facebook.com"><span class="dashicon dashicons dashicons-facebook"></span></a></li><li data-icon="twitter"><a href="https://twitter.com"><span class="dashicon dashicons dashicons-twitter"></span></a></li></ul></div></div> <!– /wp:blocks-course/team-member –> <!– wp:blocks-course/team-member –> <div class="wp-block-blocks-course-team-member"></div> <!– /wp:blocks-course/team-member –></div> <!– /wp:mywebsitename/generic-cards –></div></div> <!– /wp:kadence/column –></div></div></div> <!– /wp:kadence/rowlayout –>!jjarolim commentedon Apr 28, 2022
What do you get via parse_blocks( get_the_content() ) ? You should get a iterable array with all blocks of the page incl. according attributes?
StephenBugden commentedon Apr 30, 2022
Hi,
@jjarolim thank you for your idea, sorry for the delayed response, I had problems with my laptop.
I tried the following:
<h2>The content is </h2><p><?php echo var_dump(parse_blocks( get_the_content() )) ?>!</p>Output is as follows:
`C:\Users\Steve\Local Sites\mywebsitename6\app\public\wp-content\plugins\mywebsitename-cards\mywebsitename-cards.php:37:
array (size=5)
0 =>
array (size=5)
'blockName' => string 'core/block' (length=10)
'attrs' =>
array (size=1)
'ref' => int 4939
'innerBlocks' =>
array (size=0)
empty
'innerHTML' => string '' (length=0)
'innerContent' =>
array (size=0)
empty
1 =>
array (size=5)
'blockName' => null
'attrs' =>
array (size=0)
empty
'innerBlocks' =>
array (size=0)
empty
'innerHTML' => string '
' (length=2)
'innerContent' =>
array (size=1)
0 => string '
' (length=2)
2 =>
array (size=5)
'blockName' => string 'core/block' (length=10)
'attrs' =>
array (size=1)
'ref' => int 6275
'innerBlocks' =>
array (size=0)
empty
'innerHTML' => string '' (length=0)
'innerContent' =>
array (size=0)
empty
3 =>
array (size=5)
'blockName' => null
'attrs' =>
array (size=0)
empty
'innerBlocks' =>
array (size=0)
empty
'innerHTML' => string '
' (length=2)
'innerContent' =>
array (size=1)
0 => string '
' (length=2)
4 =>
array (size=5)
'blockName' => string 'kadence/rowlayout' (length=17)
'attrs' =>
array (size=4)
'uniqueID' => string '_91026b-c2' (length=10)
'columns' => int 1
'colLayout' => string 'equal' (length=5)
'inheritMaxWidth' => boolean true
'innerBlocks' =>
array (size=1)
0 =>
array (size=5)
...
'innerHTML' => string '
So I don't think the attributes are being stored separately and I can't understand why.
Steve
phpbits commentedon Jun 5, 2023
This would be the easier way to access the innerblocks data in the block PHP render:
Btw, I created a full guide here: https://jeffreycarandang.com/how-to-access-innerblocks-attributes-within-the-parent-block-on-the-wordpress-gutenberg-editor/. Thanks!