Last active
June 2, 2023 15:21
-
-
Save phpbits/f4dcab50f46d570b4c13a38af4be5f23 to your computer and use it in GitHub Desktop.
Access Inner Blocks on the Parent Block during the PHP Block Render : https://jeffreycarandang.com/how-to-access-innerblocks-attributes-within-the-parent-block-on-the-wordpress-gutenberg-editor/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment