• Resolved mvanboordt

    (@mvanboordt)


    Hi,

    I’m adding multiple fields to the user profile page.
    I want the user to be able to change this on the front end.
    I have managed to create a form and update it all, however I don’t want the user to change all the fields so some fields aren’t editable for the user.
    The problem is that if the user updates his edited profile all the fields that I hide for him are blank in the database. The current data is being overwritten with nothing (empty values).
    I don’t know what I’m doing wrong

    Here is my code:

    <?php global $current_user, $wp_roles;
    get_currentuserinfo();
    
    /* Load the registration file. */
    $error = array();
    /* If profile was saved, update profile. */
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
    
        /* Update user information. */
        if ( !empty( $_POST['email'] ) ){
            if (!is_email(esc_attr( $_POST['email'] )))
                $error[] = __('Het e-mailadres dat u heeft opgegeven is niet correct.', 'profile');
            elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->ID )
                $error[] = __('Dit e-mailadres is al in gebruik.', 'profile');
            else{
                wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
            }
        }
    
        if ( !empty( $_POST['first_name'] ) )
            update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first_name'] ) );
        if ( !empty( $_POST['last_name'] ) )
            update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last_name'] ) );
        if ( !empty( $_POST['initials'] ) )
            update_user_meta( $current_user->ID, 'initials', esc_attr( $_POST['initials'] ) );
    
        /* Redirect so the page will show updated info.*/
        if ( count($error) == 0 ) {
            //action hook for plugins and extra fields saving
            do_action('edit_user_profile_update', $current_user->ID);
            wp_redirect('some-page');
            exit;
        }
    }
    ?>
    <form method="POST" id="adduser" action="<?php the_permalink(); ?>" enctype="multipart/form-data">
    	<table cellpadding="0" cellspacing="0" border="0" width="350" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#271d67;">
    	<tr height="25">
    		<td><strong>First name:</strong></td><td><input class="profile_formfield" name="first_name" type="text" id="first_name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" /></td>
    	</tr>
    	<tr height="25">
    		<td><strong>Last name:</strong></td><td><input class="profile_formfield" name="last_name" type="text" id="last_name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" /></td>
    	</tr>
    	<tr height="25">
    		<td><strong>Initials :</strong></td><td><input class="profile_formfield" name="initials" type="text" id="initials" value="<?php the_author_meta( 'initials', $current_user->ID ); ?>" /></td>
    	</tr>
    
    	<p class="profile_form_submit">
    								<input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Opslaan', 'profile'); ?>" />
    								<?php wp_nonce_field( 'update-user' ) ?>
    								<input name="action" type="hidden" id="action" value="update-user" />
    							</p>
    </td>
    </tr>
    </table>
    </form>

    Hope somebody has some pointers

Viewing 1 replies (of 1 total)
  • Thread Starter mvanboordt

    (@mvanboordt)

    Okay got it working.
    For anyone who wants to know:

    do_action('edit_user_profile_update', $current_user->ID);
    This fires before the save function so all the fields that are in the database will be updated. Since there is no new data it get’s updated with empty values.

    I just deleted it.!!!

Viewing 1 replies (of 1 total)
  • The topic ‘Blank fields after update_user_meta’ is closed to new replies.