Viewing 6 replies - 1 through 6 (of 6 total)
  • @oshevans

    allow users to enter an email address for their user_login

    Use the “Username or Email” UM predefined field at your Login page
    and not the “Username” field.

    Thread Starter oshevans

    (@oshevans)

    Thanks!

    Unfortunately, that field has a different Meta Key (“Username”), so I can’t use it. I don’t really know what that meta key is used for in WP either – shouldn’t it be”user_login” anyway?

    Also, I’m unable to use a custom field with “user_login”, as it gives an error when creating the form: “Your meta key is a predefined reserved key and cannot be used”.

    Is there a way to override the Username validation (there is no option to specify a custom validation).

    • This reply was modified 11 months, 3 weeks ago by oshevans.

    @oshevans

    username is used both for user_login and user_email
    when you have the “Username or Email” UM predefined field at your Login page.

    Test is made if User entry is an email address or not
    and validation is “Unique Username/Email”.

    Thread Starter oshevans

    (@oshevans)

    username?is used both for?user_login?and?user_email
    when you have the “Username or Email” UM predefined field at your Login page.

    OK, maybe that works for login, but when I register or edit a profile “username” is not a WP meta field, so it doesn’t do anything to the underlying account.

    I will need to hook that value into the _login data.

    Thread Starter oshevans

    (@oshevans)

    If anyone has a better / more integrated solution than the one below, let me know.

    Here’s what I’ve got, based on the original code from this post, and info from @missveronica:

    /* UM PROFILE FORM - edit user_login and user_email from single username field */
    
    add_filter("um_user_profile_restricted_edit_fields","um_111921_allow_username_edit");
    function um_111921_allow_username_edit( $restricted_fields ){
      
        return array();
    }
    
    
    add_action("um_submit_form_profile","um_111921_username_validation", 1 );
    function um_111921_username_validation( $post_form ){
          
        if( isset( $post_form['username'] ) && ! empty( $post_form['username'] ) ){
            $user = wp_get_current_user();
            if( username_exists( $post_form['user_login'] ) && $post_form['username'] !== $user->user_login ){
                UM()->form()->add_error( 'user_login', __( 'Your username is already taken', 'ultimate-member' ) );
            }
        }
    }
    add_action("um_user_after_updating_profile","um_111921_update_username", 9999999, 3 );
    function um_111921_update_username( $to_update, $user_id, $args ){
        global $wpdb;
        if( isset( $to_update['username'] ) && ! empty( $to_update['username'] ) ){
    		$user = wp_get_current_user();
    		$sql = $wpdb->prepare("UPDATE {$wpdb->users} SET user_login = %s, user_email = %s WHERE ID = %d", $to_update['username'], $to_update['username'], $user_id);
    		$wpdb->query($sql);
    
    		// Log-in again.
    		wp_set_auth_cookie($user->ID);
    		wp_set_current_user($user->ID);
    		do_action('wp_login', $user->user_login, $user);
    	}
    }
    
    

    @oshevans

    For the Registration form you use the user_login and user_email fields.

    For Login form you use the username field to allow Users to use both user_login and user_email values for their identification together with their password.

    Keep your code snippet without changes for editing the user_login field from the Profile page.

    user_email field can be edited by UM default at the User Account page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Edit profile – use email address when editing username’ is closed to new replies.