Home › Forums › Front-end Issues › new value is shown only after refresh page on update_field
Hello, i want to give the user the ability to update his social media links on frontend. However, the update_field is working, but when i send the form, i have to refresh the entire page to get the new value of the field.
function feldgruppentest() {
ob_start(); // Pufferung starten
// Überprüfen, ob der Benutzer eingeloggt ist
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
// Profil ID des aktuellen Benutzers auslesen
$lwb_profile_id = get_field('lwb_profile_id', 'user_' . $current_user->ID);
}
$fieldgroup = get_field_object('lwb_social_media', $lwb_profile_id)['sub_fields'];
echo '<form method="post" id="social-media-update-form">';
foreach ($fieldgroup as $link) {
// Feldinformationen sammeln
$field_name = $link['name'];
$field_label = $link['label'];
$field_key = $link['key'];
$field_value = get_field('lwb_social_media_' . $field_name, $lwb_profile_id);
?>
<label for="<?php echo $field_name; ?>"><?php echo $field_label; ?></label>
<input type="text" name="<?php echo $field_name; ?>" id="<?php echo $field_label; ?>" value="<?php echo esc_attr($field_value); ?>" required>
<?php
}
echo '<input type="submit" name="update_social_media" value="Speichern">';
echo '</form>';
// Wenn das Formular abgesendet wurde
if (isset($_POST['update_social_media'])) {
$value = sanitize_text_field($_POST['lwb_social_media_tiktok']);
update_field('lwb_social_media_lwb_social_media_tiktok', $value, $lwb_profile_id );
echo '<p>Das Profil wurde erfolgreich gespeichert.</p>';
//wp_safe_redirect( home_url('/mein-konto/'));
} else {
echo '<p>Du musst eingeloggt sein, um dein Profil zu ändern.</p>';
}
return ob_get_clean(); // Rückgabe des gepufferten Inhalts
}
add_shortcode('Profilinformationen', 'feldgruppentest');
When you get the value of a field it is stored in a cache. This cache is not cleared when you call update_field().
In your case the conditional updating of the field should happen before you attempt to get the value of the field for display.
Move your entire block of code that starts with
if (isset($_POST['update_social_media'])) {
to before you get the field, updating happens before display. Then if the value has been updated instead of getting the value you can just skip that and us the submitted value. Then you only need to get the value it it is not present in $_POST.
You must be logged in to reply to this topic.
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.