Thanks for getting back to me!I’ve worked with lots of different CRM systems, including projects where we’ve been a Salesforce implementation partner, so I’d like to share some more info about working with ACF fields for users.
When you update a custom ACF field, there are two ways to do it: use field_name or field_key.Field_name only works if the field already exists and has a value.For example:
update_field('location', $_POST['current_country'], 'user_'.$user_id);
Field_key is a more reliable way, especially if the field may not exist or doesn’t have a value.For example:
update_field('field_5fb3785065726', $_POST['current_country'], 'user_'.$user_id);
This will help avoid problems with updating fields that may not exist.I hope this will be useful to other developers!
Your approach is almost correct, but there are a few issues to address:
The acf/update_value action does not directly pass the Google Maps field data as an associative array. You need to ensure that $value contains the data you’re expecting.
Here’s the corrected code:
function update_state_short( $value, $post_id, $field ) {
// Check if the value contains the 'state_short' key
if ( isset( $value['state_short'] ) ) {
// Update the 'state_short' ACF field with the new value
update_post_meta( $post_id, 'state_short', $value['state_short'] );
}
// Return the original value to ensure the ACF field is saved correctly
return $value;
}
// Hook into the ACF update_value action for the specific field key
add_filter( 'acf/update_value/key=field_5e5a38be195bc', 'update_state_short', 10, 3 );
This article explains how to use the acf/update_value hook in detail. It has examples and recommendations: https://www.advancedcustomfields.com/resources/acf-update_value/
Check that the “project_galleries” field in ACF is set up as a REPEATER, and inside it, “project_gallery” is configured as a gallery.
To work with Gutenberg blocks, you can try using the “get_field()” function instead of “have_rows()”. Try this:
<?php
$galleries = get_field('project_galleries');
if($galleries):
foreach($galleries as $gallery):
$images = $gallery['project_gallery'];
if($images):
foreach($images as $image): ?>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
<?php endforeach;
endif;
endforeach;
endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.