Home Forums Front-end Issues new value is shown only after refresh page on update_field


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>
    	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.

  • Hey John, thanks for answering. How can i achieve that?

  • 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.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.