Updated 25.03. : Some function names changed in the latest beta version.
ACF 5.8 Beta introduced an easy way to create your custom Gutenberg blocks. I am already using it heavily for a current project, to easily organize content and media assets.
Really powerful, when combined with Timber as well, which has been the foundation of many of my themes for years now ;)
Organizing data using ACF is nice, but sometimes you seek access to that saved block data directly. I hate it when I am confined to boundaries and the data flow is restricted or hidden. I need things to be accessible to choose the creative flow myself.
There you go, enjoy some free block data :)
I was a big skeptic, when it comes to WordPress and the new Gutenberg editor, but combined with ACF + Timber its pure magic :) Looking forward to things to come!
Just missing something here. Where do you find the block ID?
$block[‘attrs’][‘id’] holds the ID of the block.
Thanks! I understand. Seems last update broke “acf_setup_postdata”… We’ll need to find another way…
Hi, function names changed in the latest version.
// Setup postdata allowing get_field() to work. acf_setup_meta( $block['data'], $block['id'], true );
// Reset postdata. acf_reset_meta( $block['id'] );
The relevant reference code, can be found in pro/blocks.php
Yes thanks! I juts find out! :)
You saved me from going crazy with this article. Thank you!!!
Glad I did, we are all crazy enough already :) Alex
Great article Alex, helped me a lot!
I need to update a custom field inside a specific block. Do you know how update a field value inside that ‘acf_setup_meta’??
I tried using the ‘update_field’ function, but it saves the field on the post itself, not the block :(
Hi Carlos, Take a look at the pro/blocks.php. Block content is saved within the post_content. acf_parse_save_blocks & acf_parse_save_blocks_callback handle the block comment replacement. It should be a matter of getting the current post content, replacing the target block comment and saving it again. If I have some time tomorrow, I will take a closer look. Cheers Alex
acf_parse_save_blocks & acf_parse_save_blocks_callback
Get in touch
I am a full-stack developer. My expertise include:
I love programming, design and know my way around server architecture as well. I would never feel complete, with one of these missing.
I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest.
Apart from that, I am a peace loving guy who tries to have people around him that think the same. I truly believe in the principle: “If you help someone, someone will help you, when you need it."