WPUF – change user_nicename during registration and profile update

[themify_is_logged_in]

[php]

######################  Update Registration ######################

function update_your_registration( $user_id, $form_id, $form_setting ) {

	// check the right FORM
	if ( $form_id != 1358 ) {
	return;
	}
	// make sure user ID exists.
	if ( ! $user_id ) {
	return;
	}

	if ( isset( $_POST['nickname'] ) ) {

	wp_update_user( array(
			'ID'            => $user_id,
			'display_name'	=> $_POST['nickname'],
			'user_nicename' => sanitize_user( $_POST['nickname'] )
		)
					  );
}	

}
add_action( 'wpuf_after_register', 'update_your_registration', 10, 3 );

######################  Update Profile ######################

function update_your_profile( $user_id, $form_id, $form_setting ) {
	// check the right FORM
	if ( $form_id != 1382 ) {
	return;
	}
	// make sure user ID exists.
	if ( ! $user_id ) {
	return;
	}

	if ( isset( $_POST['nickname'] ) ) {

	wp_update_user( array(
			'ID'            => $user_id,
			'display_name'	=> $_POST['nickname'],
			'user_nicename' => sanitize_user( $_POST['nickname'] )
		)
					  );
}	

}
add_action( 'wpuf_update_profile', 'update_your_profile', 10, 3 );

[/php]

Verweis: https://github.com/weDevsOfficial/wp-user-frontend/issues/53
[/themify_is_logged_in]

######################  Update Registration ######################

function get_random_unique_username(){
    $user_exists = 1;
    do {
       $rnd_str = sprintf( "%0d", mt_rand( 1000000000, 9999999999 ) );
       $user_exists = username_exists( $rnd_str );
   } while( $user_exists > 0 );
   return $rnd_str;
}
 
 
function update_your_registration( $user_id, $form_id, $form_setting ) {
     
    // check the right FORM
    if ( $form_id != 327 ) {
    return;
    }
    // make sure user ID exists.
    if ( ! $user_id ) {
    return;
    }
     
    if ( isset( $_POST['nickname'] ) ) {
 
    wp_update_user( array(
            'ID'            => $user_id,
            'display_name'  => $_POST['nickname'],
            'user_nicename' => sanitize_user( $_POST['nickname'] )
	)
        );
		
		global $wpdb;
		$wpdb->update(
			$wpdb->users,
			['user_login' => get_random_unique_username()],
			['ID' => $user_id]
);
}   
     
}
add_action( 'wpuf_after_register', 'update_your_registration', 10, 3 );