Last active
April 30, 2024 06:14
-
-
Save sabrina-zeidan/e65252c609a69cf9c76655fa96283b75 to your computer and use it in GitHub Desktop.
Get ACF field value from Gutenberg block [WordPress]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what about update?
do you have sample code for updating ?
update_acf_block_data
if you have please share