• Resolved aymanibrahim77

    (@aymanibrahim77)


    hello
    in UM registration form want to remove username field and use mobile number field as username
    how to do that please
    regards

Viewing 15 replies - 1 through 15 (of 28 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    You can try this code snippet below. Ensure that you’re using the Mobile Number field with the meta key/field name mobile_number.

    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'] ) ){
            global $wpdb;
    
        $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->usermeta} WHERE meta_key = 'mobile_number' AND meta_value = %s", $post_form['mobile_number'] ) );
            if( $wpdb->num_rows > 0 )  {
                UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) );
    		}
    	}  
    
    }
    
    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));
    
    }

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp thank you for you reply
    note I am using code to let logged in user can add new users
    unfortunate I got this message and i can not register the user
    but at the same time new user was created has the mobile number

    please advise
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Please try this one:

    add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 1 );
    function um_090321_mobile_number_zero_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' ) );
            }
            
            if ( strpos( $post_form['mobile_number'], '0') === 0) {
                UM()->form()->add_error('mobile_number', __( 'Please enter mobile number without starting from 0', 'ultimate-member' ) );
    	    }
        }
    }
    
    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));
    
    }
    

    Regards,

    • This reply was modified 3 years, 2 months ago by Champ Camba.
    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp thank you for your reply

    I got this message
    Please enter mobile number without starting from 0
    also I found that in email field it drag the current username and fill the filed with it automatically

    after I removed the 0 and delete the username of the current user it works
    so please advise
    1- how to let normally add the 0
    2- stopping drag the username and inserting it inside email field in case the undone registration

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    1. Did you mean when you enter 123456789, it should save as 0123456789?
    2. It’s not clear. What do you mean by dragging the username? Do you have a screenshot?

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp
    for point 1
    I entered normal mobile number 0123456789 but form reject it because it started with 0 and when I make it 123456789 for accept and that not right it should start with 0

    for point 2
    as example my username is user and I tried to add new user with mobile number 0123456789 and I left email field empty and for any reason registration form refuse the registration and page is refreshed and I got error
    then I found the email field in stead being empty I found on it my username “user”

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    1. Please try this one:

    add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 1 );
    function um_090321_mobile_number_zero_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' ) );
            }
            
            
        }
    }
    
    add_action("um_user_register","um_090321_after_register_complete", -1, 2 );
    function um_090321_after_register_complete( $user_id, $args ){
       
        global $wpdb;
    
    if ( strpos( $args['mobile_number'], '0') !== 0) {
       $args['mobile_number'] = "0". $args['mobile_number'];
    }
        $wpdb->update( $wpdb->users, array('user_login' => $args['mobile_number'] ), array('ID' => $user_id));
    
    }

    2. Sorry but I’m still confused. Do you want to make the email field empty and when you submit the Register form, it should not be shown as required? It will be helpful if you can provide a screenshot of the issue so I can visualize it clearly.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp
    for point 1 the result is
    a- if mobile number in this format 0123456789 the registration continue and the created username = 0123456789
    and
    b- if mobile number in this format 123456789 the registration continue and the created username = 0123456789

    for a- it s okay
    for b- need to refuse continue registration with message say mobile should start with 0 (I will determine number for letter for this field thats why I need to be in this way)

    regarding the second point this is images for it
    https://www.mediafire.com/file/r6ea1m30lijp0og/1.png/file
    https://www.mediafire.com/file/j6yvu3trzm0vtvg/2.png/file
    best regards

    @aymanibrahim77

    Try this modified code snippet:

    add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 1 );
    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', __( 'Mobile Number must begin with 0', 'ultimate-member' ) );
            }
            if( username_exists( $post_form['mobile_number'] ) )  {
                UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) );
            }        
        }
    }
    
    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));
    
    }

    I have changed the verification if the mobile number exists (removing the “0”)
    I have added the submit test for first digit is a “0”
    Removed the addition of a missing “0” before updatig the database.
    Final change doing the missing first “0” before verification of the existance of the number.

    • This reply was modified 3 years, 2 months ago by missveronica.
    • This reply was modified 3 years, 2 months ago by missveronica.
    • This reply was modified 3 years, 2 months ago by missveronica.
    • This reply was modified 3 years, 2 months ago by missveronica.
    • This reply was modified 3 years, 2 months ago by missveronica.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    1. You need to prevent users from entering a mobile number that starts with 0? You can try this code:

    add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 1 );
    function um_090321_mobile_number_zero_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' ) );
            }
            if ( strpos( $post_form['mobile_number'], '0') !== 0) {
               UM()->form()->add_error('mobile_number', __( 'Mobile Number should start with a zero', 'ultimate-member' ) );
           }
            
        }
    }
    
    add_action("um_user_register","um_090321_after_register_complete", -1, 2 );
    function um_090321_after_register_complete( $user_id, $args ){
       
        global $wpdb;
    
    if ( strpos( $args['mobile_number'], '0') !== 0) {
       $args['mobile_number'] = "0". $args['mobile_number'];
    }
        $wpdb->update( $wpdb->users, array('user_login' => $args['mobile_number'] ), array('ID' => $user_id));
    
    }

    2. You can try the following code snippet to clear the Register form after the submission or on the first page reload:

    add_action( 'um_main_register_fields', function(){
         if( is_user_logged_in() ){
           echo "<script type='text/javascript'>";
           echo "jQuery(document).ready(function(){";
           echo "setTimeout( function(){ jQuery('.um-register:not(.um-err)').find(\"input[type=text],input[type=number], textarea\").val(\"\"); }, 600 );";
           echo "});";
           echo "</script>";
         }
     });

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp thanks for your reply
    @missveronicatv thanks fro your helping
    regarding point 1
    I used the code that provided by @missveronicatv and it works as I want

    regarding point 2
    I already use this code but
    a- it clear the registration form only after the complete registration
    but
    b- it didn’t do any thing if registration not completed for any error and show fields “email, password” as I cleared in the previous images, message
    so please advise

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    2. I think this is a browser setting issue. Does it populate the email and password using other browsers?

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Hi @champsupertramp you were right I test it in other browser and didn’t do the same
    thanks

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know. I’m marking this as resolved now.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Hi @champsupertramp
    this is the last code used to use mobile field as username

    add_action("um_submit_form_register","um_090321_mobile_number_zero_validation", 1 );
    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', __( 'Mobile Number must begin with 0', 'ultimate-member' ) );
            }
            if( username_exists( $post_form['mobile_number'] ) )  {
                UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) );
            }        
        }
    }
    
    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));
    
    }

    yes it create username = mobile field in backend user edit page as ex 012345689 but do another wrong things
    in frontend in account page and in profile page and when use [um_field_data field=user_login] to show username
    all these things show username as (firstname+secondname “if I didn’t fill email field”) and can show username as (email “if I fill email filed”) and that instead of the right thing “0123456789”

    so please advise how to fix this
    best regards

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘mobile field in stead of username’ is closed to new replies.