make another field create the username
-
hello
is it possible to do the next
1- will not add username field in registration form
2- will add mobile number as required
3- will add email and first, last name fields
after finishing registration then the created username will be the mobile numberhow to do that ?
best regards
-
Hi @makmerghen
You can try these code snippets below:
// Add validation if the mobile number is already in use. add_action("um_submit_form_register","um_090321_mobile_number_validation"); function um_090321_mobile_number_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if( username_exists( $post_form['mobile_number'] ) ) { UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) ); } } } // Update the user_login and save the mobile number as username. add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => $args['mobile_number'] ), array('ID' => $user_id)); }
The above code will validate if the mobile is already used as a username. When it passes the validation, it will save the mobile number as the username.
You can add the code snippet to your theme/child-theme’s functions.php file or use the Code Snippet plugin to run the code.
Regards,
than you very much and God bless you
only one thing please help to do
I used some plugin separate UM mobile field to 2 fields country code and mobile number so I add it like this +20 in separate field ad 125245355 in mobile field
so the created username = 125245355I just want to add 0 at the beginning of the created username so for this example the created username become 0125245355 instead of 125245355
thanks
Hi @makmerghen
Please try this updated code:
add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => "0".$args['mobile_number'] ), array('ID' => $user_id)); }
Regards,
don’t know how to thank you you are wonderful
best regardsThanks for letting us know. I’m marking this as resolved now.
Regards,
hello
we used the next code to use mobile number field as username and to add 0 to the entered number// Add validation if the mobile number is already in use. add_action("um_submit_form_register","um_090321_mobile_number_validation"); function um_090321_mobile_number_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if( username_exists( $post_form['mobile_number'] ) ) { UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) ); } } } // Update the user_login and save the mobile number as username. add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => "0".$args['mobile_number'] ), array('ID' => $user_id)); }
would you please help to do the next
need if the entered number don’t start with 0 then to add 0 ex. number is 11342334 then username will be 011342334
and
if the entered number do start with 0 then will not add 0 ex. number is 0123456 then user name will be the same 0123456best regards
Hi @makmerghen
Please try this:
add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $mobile_number = $args['mobile_number']; if ( strpos( $mobile_number, '0') !== 0) { $mobile_number = "0" . $mobile_number; } $wpdb->update( $wpdb->users, array('user_login' => $mobile_number ), array('ID' => $user_id)); }
Regards,
thank you for your message
when add mobile number like this 11342334 then created username became 011342334 and every thing work right (page was refreshed and message of successfully creating new user was appear
and
when add mobile number like this 011342334 I got message under mobile filed saying ‘Mobile Number already registered’ and the form was look like didn’t finish the registration
although I found that the account was created and username became 011342334 as I wantso please if you have idea to fix that please advise
or
the easiest way to add code to say not allowed to start by 0 or to automatically remove the first 0 from the number while writing the number if the number started by 0best regards
- This reply was modified 3 years ago by makmerghen.
Hi @makmerghen
You said that you successfully registered the mobile number
011342334
and then you tried registering the same mobile number again so the issue is there. Try registering another mobile number.Regards,
no dear
first I register 1144444 and it work well
then I delete that registered account
then I used 011444444
so the issue not from existing of the same username because I already removed it
best regardsHi @makmerghen
Does the user account exist with the mobile number
011444444
in WP Admin > Users?Regards,
no I deleted first before trying to create new account
let me explain this point again in clear waywhen add mobile number like this 11342334 then created username became 011342334 and every thing work right (page was refreshed and message of successfully creating new user was appear and when add another mobile number like this 012000000 got message under mobile filed saying ‘Mobile Number already registered’ and the form was look like didn’t finish the registration although I found that the account was created and username became 012000000 as I want so please if you have idea to fix that please advise or the easiest way to add code to say not allowed to start by 0 or to automatically remove the first 0 from the number while writing the number if the number started by 0
Hi @makmerghen
You can try showing an error when the number starts with 0.
add_action("um_submit_form_register","um_090321_mobile_number_zero_validation"); function um_090321_mobile_number_zero_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if ( strpos( $post_form['mobile_number'], '0') === 0) { UM()->form()->add_error('mobile_number', __( 'Please enter mobile number without starting from 0', 'ultimate-member' ) ); } } }
Regards,
Hi dear
unfortunately I got the next
I add mobile number with 0 at beginning 01222222 and continue registration the result was
1- registration page refreshed as if the registration didn’t complete with the message of Please enter mobile number without starting from 0
2- account created with user name 0122222
step 2 should not happened
so please advise
and here is the all code after adding the last part you sent// Add validation if the mobile number is already in use. add_action("um_submit_form_register","um_090321_mobile_number_validation"); function um_090321_mobile_number_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if( username_exists( $post_form['mobile_number'] ) ) { UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) ); } } } // Update the user_login and save the mobile number as username. add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => "0".$args['mobile_number'] ), array('ID' => $user_id)); } // Add validation if the mobile number started with 0 add_action("um_submit_form_register","um_090321_mobile_number_zero_validation"); function um_090321_mobile_number_zero_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if ( strpos( $post_form['mobile_number'], '0') === 0) { UM()->form()->add_error('mobile_number', __( 'Please enter mobile number without starting from 0', 'ultimate-member' ) ); } } }
best regards
Hi @makmerghen
Please try this code snippet:
// Add validation if the mobile number is already in use. add_action("um_submit_form_register","um_090321_mobile_number_validation", 1); function um_090321_mobile_number_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if( username_exists( "0".$post_form['mobile_number'] ) ) { UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) ); } } } // Update the user_login and save the mobile number as username. add_action("um_user_register","um_090321_after_register_complete", -1, 2 ); function um_090321_after_register_complete( $user_id, $args ){ global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => "0".$args['mobile_number'] ), array('ID' => $user_id)); } // Add validation if the mobile number started with 0 add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 2); function um_090321_mobile_number_zero_validation( $post_form ){ if( isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){ if ( strpos( $post_form['mobile_number'], '0') === 0) { UM()->form()->add_error('mobile_number', __( 'Please enter mobile number without starting from 0', 'ultimate-member' ) ); } } }
Regards,
- The topic ‘make another field create the username’ is closed to new replies.