Support

Account

Home Forums ACF PRO Front-end Update User Meta ACF / WP User Meta Sync

Solved

Front-end Update User Meta ACF / WP User Meta Sync

  • Hello,

    I am struggling a bit with a user data update form in my font-end.

    – The profile fields group fields shows in my back-end user profile
    – I cannot get my wp_update_user( $args ); to actually update the user meta, although the acf fields update fine.

    .. I think my function is not hooking to the form ..

    My form:

    
    <?php
    
    acf_form_head();
    
    $avatar_group = get_field( 'sys_avatar_field_group', 'option' );
    $current_user = wp_get_current_user();
    
    $user_id = get_current_user_id();
    $user_acf_prefix = 'user_';
    $user_id_prefixed = $user_acf_prefix . $user_id;
    
    $user_info = get_userdata($user_id);
    $member_email = $user_info->user_email;
    
    $options = array(
    	'post_id' => 'user_'.$current_user->ID,
    	'field_groups' => array(26734, $avatar_group),
    	'submit_value' => 'Update Profile');
    
    ?>
    
    <?php acf_form( $options );?>
    

    .. and my function in a global network activated (multisite) plugin:

    
    /** User Meta Update ------------------------------------------------ **/
    
    function update_member_data( $post_id ) {
    	if( empty($_POST['acf']) ) {
    		return;
    	}
    	
    	if( is_admin() ) {
    		return;
    	}
    	
    	if( $_POST['post_id'] != 'new' ) {
    		
    		$memberEmail = $_POST['acf']['field_5e880d5caa23d'];
    		$memberFirstname = $_POST['acf']['field_5e880c9b0e92d'];
    		$memberLastname = $_POST['acf']['field_5e880d0daa23b'];
    		$memberNickname = $_POST['acf']['field_5e880d3baa23c'];
    		$user_id = str_replace("user_", "", $post_id);
    		
    		if (isset($memberEmail)) {
    			if (email_exists( $memberEmail )){
    				update_field('field_5e880d5caa23d', get_the_author_meta('user_email',$user_id), $post_id);
    			} else {
    				$args = array(
    					'ID'         => $user_id,
    					'first_name' => esc_attr( $memberFirstname ),
    					'last_name' => esc_attr( $memberLastname ),
    					'nickname' => esc_attr( $memberNickname ),
    					'user_email' => esc_attr( $memberEmail )
    				);            
    				wp_update_user( $args );
    			}  
    		} 	
    	}
    	
    	return $post_id;
    }
    add_action('acf/save_post', 'update_member_data', 20);
    

    Help would be appreciated.

  • OK, got it.

    There are a couple of issues here, including zero validation.

    My Form:

    
    <?php
    
    acf_form_head();
    
    $avatar_group = get_field( 'sys_avatar_field_group', 'option' );
    $current_user = wp_get_current_user();
    
    $user_id = get_current_user_id();
    $user_acf_prefix = 'user_';
    $user_id_prefixed = $user_acf_prefix . $user_id;
    
    $user_info = get_userdata($user_id);
    $member_email = $user_info->user_email;
    
    $options = array(
    	'post_id' => 'user_'.$current_user->ID,
    	'field_groups' => array(group_5e880597057c0, $avatar_group),
    	'submit_value' => 'Update Profile'
    );
    
    ?>
    

    Validation:

    
    /** User Meta Update Validation ------------------------------------------------ **/
    
    add_action('acf/validate_save_post', 'validate_user_input_data');
    function validate_user_input_data() {
    
      $email = $_POST['acf']['field_5e880d5caa23d'];
      $user_info = get_userdata($user_id);
      $current_email = $user_info->user_email;
    
      if ( empty($_POST['acf']['field_5e880d5caa23d']) ) {
        acf_add_validation_error( 'acf[field_5e880d5caa23d]', 'You need to specify an email address' );
      }
    
      elseif ( email_exists($email) && $email != $current_email ) {
        acf_add_validation_error( 'acf[field_5e880d5caa23d]', 'The email address you entered is not available' );
      }
    
    }
    

    Update function:

    
    /** User Meta Update ------------------------------------------------ **/
    
    function update_member_data( $post_id ) {
    
    	if( empty($_POST['acf']) ) {
    		return;
    	}
    	
    	if( is_admin() ) {
    		return;
    	}
    	
    	if( $_POST['post_id'] != 'new' ) {
    		
    		$memberEmail = $_POST['acf']['field_5e880d5caa23d'];
    		$user_id = str_replace("user_", "", $post_id);
    		
        $args = array(
         'ID'         => $user_id,
         'user_email' => esc_attr($memberEmail)
       );
        wp_update_user($args);
    
        $memberFirstname = $_POST['acf']['field_5e880c9b0e92d'];
        $memberLastname = $_POST['acf']['field_5e880d0daa23b'];
        $memberNickname = $_POST['acf']['field_5e880d3baa23c'];
        $memberDisplayname = $_POST['acf']['field_5e886a3273989'];
    
        update_user_meta($user_id, 'first_name', $_POST['acf']['field_5e880c9b0e92d'] );
        update_user_meta($user_id, 'last_name', $_POST['acf']['field_5e880d0daa23b'] );
        update_user_meta($user_id, 'nickname', $_POST['acf']['field_5e880d3baa23c'] );
        update_user_meta($user_id, 'display_name', $_POST['acf']['field_5e886a3273989'] );
      }
      return $post_id;
    }
    add_action('acf/save_post', 'update_member_data', 20);
    

    Note: wp_update_user does not handle meta updates, you need to use update_user_meta.

    Graeme

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

The topic ‘Front-end Update User Meta ACF / WP User Meta Sync’ is closed to new replies.