How to Force Users to Login Using Their Email Address?

By default, UsersWP allows users to login either via their usernames or using their email addresses. This is in sync with the default WordPress behavior, and works out of the box with no configuration required. 

However, if you prefer, you can disable login by username feature, and compel your users to login by entering their email addresses.  

Code Snippet to Disable Login Using Usernames

You can use the following code snippet to make sure that users can only login using their email addresses and not usernames: 

We recommend using the Code Snippets plugin for adding snippets to your site. Alternatively, you might also paste the code snippet directly in the functions.php file of your WordPress theme.  

add_filter('uwp_form_input_text_username', 'uwp_form_input_text_username_cb', 10, 4);
function uwp_form_input_text_username_cb($html, $field, $value, $form_type){ 
	if(isset($field->form_type) && $field->form_type == 'login' && $field->htmlvar_name == 'username'){
		$html = aui()->input(array(
		    'type'  =>  'email',
		    'id'    =>  $field->htmlvar_name,
		    'name'    =>  $field->htmlvar_name,
		    'placeholder'   => __("Email *", 'userswp'),
		    'title'   => __("Email", 'userswp'),
		    'value' =>  $value,
		    'required'  => $field->is_required,
		    'validation_text' => __($field->required_msg, 'userswp'),
		    'help_text' => uwp_get_field_description($field),
		    'label' => is_admin() ? '' : __("Email", 'userswp'),
	    ));
    }
    return $html;
}

The above code will prevent users from logging in using their usernames. Instead, users will now need to enter their email addresses in order to login. 

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.