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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Parse blocks from post content $blocks = parse_blocks($your_post_content); $collect = array(); // Loop through the blocks foreach($blocks as $block){ //Setup global block post data context // before: acf_setup_postdata acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true ); // Get ACF fields $fields = get_fields(); // I am using this to organize my assets. // Each block of mine has a unique identifier as its first field: // $uid = $block['attrs']['data'][array_keys($block['attrs']['data'])[0]] // I would do: // $collect[$uid] = $fields; // Collection of fields using the block id. $collect[$block['attrs']['id']] = $fields; // Restore global context // before: acf_reset_postdata acf_reset_meta( $block['attrs']['id'] ); } |
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!
Cheers
Alex
Extended example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$collect = []; $blocks = parse_blocks($your->post_content); foreach($blocks as $block){ if( isset($block['attrs']['data']) && !empty($block['attrs']['data'][array_keys($block['attrs']['data'])[0]])){ acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true ); $fields = get_fields(); acf_reset_meta( $block['attrs']['id'] ); $collect[$block['attrs']['data'][array_keys($block['attrs']['data'])[0]]] = array('render' => render_block( $block ), 'field' => $fields, 'block' => $block ); }else{ $collect['main'] .= render_block( $block ); } } |
The $collect array will hold all data, including all ACF fields. You will have full access to any field, including repeater fields. The $collect[‘main’] will just collect the standard post content.
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."
Just missing something here. Where do you find the block ID?
$block[‘attrs’][‘id’] holds the ID of the block.
Regards
Alex
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
Regards
Alex
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
Hi Alex
Thanks for taking a stab at this. Don’t know why this have to be this difficult.
I’m trying to grab a field within a repeater from another block, but still on the same page.
I’m simply trying to output the titles of each field in a repeater from a different block.
How do I do that?
Thanks SO much for any assistance you can throw my way.
Hi Jake,
will whip up an example, when I have the time tomorrow.
Cheers
Alex
Wow, that would be amazing. Thanks Alex
:)
Hey Alex
Don’t mean to be pushy, but did you happen to figure out something? No worries if not.
Hey Jake,
have not forgotten about you. Had no time to do it yet, but its on my agenda. Hope to find some time today or tomorrow.
Later
Alex
Added an extended example above. I am using the same setup on a client website to pull in repeater field data.
Cheers
Alex
That’s very nice of you, thanks a lot.
Help a noob out, how do I make use of it?
Do I
var_dump($collect)
to output the array? If so, that just doesarray(0) { }
Have you passed in the correct post_content ?
$your->post_content is just a placeholder!
I understand how the basis of this works. I changed the parse_blocks to have my post info in it. BUT it is throwing an error on page:
Warning: key() expects parameter 1 to be array, null given
Any help would be appreciated.
Hi Chad,
parse_blocks parses the post content. So $post->post_content or $post[‘post_content’], depending on the way your posts or post have been queried.
We have created a custom ACF block with title and description.
How do I display the info on another page like home when needed as a featured content.
Where to add the above code to functions.php file or as a plugin?
When we call the acf field, How do i call the field using this code.
Sorry as this topic sound bit advanced to me but i needed to achieve the output.
All depends, the above code only extracts the raw data.
Including the data from a post / page or other post_type into your frontpage is similar, but also more involved and nothing generic. If you need help to integrate something like that, we can arrange a meet and greet on Skype to talk this through.
Regards
Alex