• In other themes I have checked (also some block themes), I can get the block attributes for example

    add_filter( ‘render_block’, etc)
    $myAttr = $block[‘attrs’];

    But in Twenty Twenty Three, attrs are not defined.

    • This topic was modified 1 year, 11 months ago by espenmn.
Viewing 7 replies - 1 through 7 (of 7 total)
  • add_filter( ‘render_block’, etc)
    $myAttr = $block[‘attrs’];

    But in Twenty Twenty Three, attrs are not defined.

    Hi @espenmn can you please provide more information about what you want to achieve exactly with the code?

    Thread Starter espenmn

    (@espenmn)

    I am making Gutenberg blocks, and also some settings for ‘all of them’.
    From what I know, it should be possible to get the attributes of the block in my render function. In all other themes I tested, I can get the attributes of the block by $block[‘attrs’]; but in TwentyTwentyTree attrs are undefined.

    I will use the attrs of the block to change/edit/disable/modify the rendering of the blocks.

    For example, the settings could be ‘disabled’, in which case the rendering returns nothing, it could be ‘promoted’, in which case the rendering could be adding a css class or some javascript or something else. Since I am adding the same to many blocks, it would be ok to do it in the rendering part.

    @espenmn – Can you share your code? Do you have a repository or somewhere it can be tested? There’s really not enough to go on.

    I’ve tested one of my custom blocks and haven’t had any trouble accessing attributes in the render callback, regardless of the theme. Also note that the default attributes set in block.json will not be passed to the callback, only those that are saved will.

    Thread Starter espenmn

    (@espenmn)

    The code is here: https://github.com/espenmn/medialog-hideblock/blob/main/src/init.php#L73

    (without the check, I got an error)

    I didn’t have any trouble with this while testing with the Twenty Twenty-Three theme and the latest WordPress 6.3 release candidate. Everything worked as it should and I tested accessing the attributes to make sure they were correctly being passed along. I also tested against a few other themes without issue.

    Honestly, the theme you’re using shouldn’t affect this at all unless that theme is doing something really weird, which shouldn’t happen with a core theme like TT3.

    If you haven’t done so, I’d test against a 100% clean copy of TT3 with no other plugins activated. Maybe even a fresh WP install to be on the safe side.

    Also, I rewrote your medialog_block_wrapper() function to simplify it (it was a bit confusing). This has no bearing on whether it works with TT3. Feel free to use it or repurpose it:

    function medialog_block_wrapper( $block_content, $block ) {
    
    	if ( isset( $block['attrs']['disableBlock'] ) && $block['attrs']['disableBlock'] ) {
    		return '';
    	}
    
    	return $block_content;
    }
    Thread Starter espenmn

    (@espenmn)

    Thanks a lot, I have updated and removed ‘all other things’, and I don’t get the ‘Undefined’ error.

    Just to be sure: If a block has NO attributes (saved), or if one just changed from a non-block theme: Is there any reason this could happen (because I think I disabled all plugins when I tested, but I used a site that ‘came from pre 6’).

    If a block instance has no saved attributes, there shouldn’t be any attributes passed to the render filter.

    As for changing themes, regardless of type, it shouldn’t affect anything to do with this (at least, I can’t think of any reason). In general, the theme shouldn’t matter at all.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Missing $block[‘attrs’] (in blocks)’ is closed to new replies.