Skip to content

Instantly share code, notes, and snippets.

@dgoze
Forked from yesnosurely/get_field_from_block.php
Created August 2, 2024 00:02
Show Gist options
  • Save dgoze/257d1c0703dd8f07d2eb14410ec6bf00 to your computer and use it in GitHub Desktop.
Save dgoze/257d1c0703dd8f07d2eb14410ec6bf00 to your computer and use it in GitHub Desktop.
get acf field from gutenberg block [php, wp, acf, wordpress]
<?php
function get_field_from_block($post_id, $block_name, $field_name){
$block_name = 'acf/' . $block_name;
$post = get_post($post_id);
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach ($blocks as $block){
if ($block['attrs']['name'] == $block_name){
$data = $block['attrs']['data'][$field_name];
break;
}
}
}
return $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment