acf_get_block_meta_values_to_save()
Iterates over blocks in post content and retrieves values that need to be saved to post meta.
No Hooks.
Returns
Array. An array containing the field values that need to be saved.
Usage
acf_get_block_meta_values_to_save( $content );
- $content(string)
- The content saved for the post.
Default:''
Changelog
| Since 6.3 | Introduced. |
acf_get_block_meta_values_to_save() acf get block meta values to save code ACF 6.8.8
function acf_get_block_meta_values_to_save( $content = '' ) {
$meta_values = array();
// Bail early if not in a format we expect or if it has no blocks.
if ( ! is_string( $content ) || empty( $content ) || ! has_blocks( $content ) ) {
return $meta_values;
}
$blocks = parse_blocks( $content );
// Bail if no blocks to save.
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
return $meta_values;
}
foreach ( $blocks as $block ) {
// Verify this is an ACF block that should save to meta.
if ( ! acf_block_uses_post_meta( $block['attrs'] ) ) {
continue;
}
// We need a block ID to retrieve the values from cache.
$block_id = ! empty( $block['attrs']['id'] ) ? $block['attrs']['id'] : false;
if ( ! $block_id ) {
continue;
}
// Verify that we have values for this block.
$store = acf_get_store( 'block-meta-values' );
if ( ! $store->has( $block_id ) ) {
continue;
}
// Get the values and remove from cache.
$block_values = $store->get( $block_id );
$store->remove( $block_id );
$meta_values = array_merge( $meta_values, $block_values );
}
return $meta_values;
}