How to create users without Email in WordPress ?

users wordpress without email

When you set up course –learndash course or Woo sensei or Learnpress , you may wish to have a setup where a student can be enrolled on a course and have a login, without the student having an email address? For example , the group leader just issues them a username and password to login and the student ( group member ) should be able to take up the course

You can also use our LearnDash custom user enrolment plugin for importing and assigning to course

Code to suppress empty email errors & removal of email input validation

// This will suppress empty email errors when submitting the user form
add_action('user_profile_update_errors', 'my_user_profile_update_errors', 10, 3 );
function my_user_profile_update_errors($errors, $update, $user) {
$errors->remove('empty_email');
}

// This will remove javascript required validation for email input
// It will also remove the '(required)' text in the label
// Works for new user, user profile and edit user forms
add_action('user_new_form', 'my_user_new_form', 10, 1);
add_action('show_user_profile', 'my_user_new_form', 10, 1);
add_action('edit_user_profile', 'my_user_new_form', 10, 1);
function my_user_new_form($form_type) {
?>
<script type="text/javascript">
jQuery('#email').closest('tr').removeClass('form-required').find('.description').remove();
// Uncheck send new user email option by default
<?php if (isset($form_type) && $form_type === 'add-new-user') : ?>
jQuery('#send_user_notification').removeAttr('checked');
<?php endif; ?>
</script>
<?php
}

How to Let Users Login to WordPress Using an Email Address

Letting users login to WordPress through his/her email is fantastic idea because it is customary to forgot user but not the email address . So ,you can give users the option to login to your site with their email address as well.

Changes Required for Adding Email Login with Code

Remove the default authentication rights in WordPress by adding the following snippet to your functions.php file:

//remove wordpress authentication
remove_filter('authenticate', 'wp_authenticate_username_password', 20);

Next, add your own authentication by adding code given below (courtesy Nishant Kumar at The Binary ) to your functions.php files:

add_filter('authenticate', function($user, $email, $password){
    //Check for empty fields
        if(empty($email) || empty ($password)){        
            //create new error object and add errors to it.
            $error = new WP_Error();
            if(empty($email)){ //No email
                $error->add('empty_username', __('<strong>ERROR</strong>: Email field is empty.'));
            }
            else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
                $error->add('invalid_username', __('<strong>ERROR</strong>: Email is invalid.'));
            }
            if(empty($password)){ //No password
                $error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));
            }
            return $error;
        }
        //Check if user exists in WordPress database
        $user = get_user_by('email', $email);
        //bad email
        if(!$user){
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }
        else{ //check password
            if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password
                $error = new WP_Error();
                $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
                return $error;
            }else{
                return $user; //passed
            }
        }
}, 20, 3);