Skip to content

Instantly share code, notes, and snippets.

@sabrina-zeidan
Last active April 30, 2024 06:14
Show Gist options
  • Save sabrina-zeidan/e65252c609a69cf9c76655fa96283b75 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/e65252c609a69cf9c76655fa96283b75 to your computer and use it in GitHub Desktop.
Get ACF field value from Gutenberg block [WordPress]
// Get ACF value whetherit comes from regular ACF or ACF block
// Useful if you need to access ACF fileds that are in block as regular fields
// Usage: get_acf_block_data($post, 'acf/talk-description', 'talk_description' )
function get_acf_block_data($post, $block_name = 'acf/default-block-name', $field_name = "" ){
$content = "";
if ( has_blocks( $post->post_content ) && !empty($field_name )) {
$blocks = parse_blocks( $post->post_content );
foreach($blocks as $block){
if ( $block['blockName'] === $block_name ) {
if(isset($block["attrs"]["data"][$field_name ])){
$content = $block["attrs"]["data"][$field_name ];
}
}
}
}
return $content;
}
@masimota
Copy link

what about update?
do you have sample code for updating ?
update_acf_block_data
if you have please share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment