Skip to content
Menu

WPUF – change user_nicename during registration and profile update

WPUF – change user_nicename during registration and profile update

###################### 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 );