
How to Customize the default login page and automatically enroll a user in LearnDash course
Note: Here is screenshot of code and the image for adding user role to the default WordPress login page.
add_action( 'register_form', 'pookidevs_register_form' );
function pookidevs_register_form() {
global $wp_roles;
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'] ) ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';}
}
echo '</select>'
}
//2. Add validation. In this case, we make sure is required.
add_filter( 'registration_errors', 'pookidevs_registration_errors', 10, 3 );
function pookidevs_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( '<strong>ERROR</strong>: Please add a role to continou.', 'pookidevs' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'pookidevs_user_register' );
function pookidevs_user_register( $user_id ) {$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}

//4: Enroll in course

Do you want to customize the default login page and automatically enroll a user in LearnDash course by adding a user role selection element to the custom WordPress login page for your website so that the user can select the role with which he wants to register and enroll in the learndash course automatically?
If you run a WordPress membership site like the one with learndash where you enroll students, then many of your users will often see the login page. Customizing the default WordPress login page allows you to offer a better user experience.
In this article, we will show you how to customize the default login page and automatically enroll a user in LearnDash course and add a user role selection element to the default login page on your WordPress site.
Here how the default WordPress login screen looks like. You can customize this screen by using the following plugin.

If you want the users to select their role on the login page. WordPress allows you to create a custom user role section element in your Login Page.- and here’s how you can do it.
Step 1: Add a new form element
Let’s create a new form element. Include the following lines of code using Code Snippets. which is an easy, clean and simple way to run PHP code snippets on your site. It removes the need to add custom snippets to your theme’s functions.php file
This code demonstrates how to add a new field to the registration form. Keep in mind that this won’t be saved automatically. You will still need to set up validation rules and manually handle the saving of the additional form fields.
add_action( 'register_form', 'pookidevs_register_form' );
function pookidevs_register_form() {
global $wp_roles;
echo '
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'] ) ) {
echo ''.$value['name'].'';
}
}
echo ''
}

Step 2: Add validation
The filtered WP_Error object may, for example, contain errors for an invalid or existing username or email address. A WP_Error object should always be returned, but may or may not contain errors.
If any errors are present in $errors, this will abort the user’s registration.
//2. Add validation. In this case, we make sure is required.
add_filter( 'registration_errors', 'pookidevs_registration_errors', 10, 3 );
function pookidevs_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( 'ERROR: Please add a role to continou.', 'pookidevs' ) );
}
return $errors;
}

Step 3: Finally, save our extra registration user meta.
This action allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.
Typically, this function is used for saving additional user meta passed by custom registration forms.
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'pookidevs_user_register' );
function pookidevs_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}

Here is what the actual page looks like after adding this code to the snippets.

Step 4: Enroll in course
Once the user is registered with a specific selected role then with this snippet user will be automatically enrolled into the course

We hope this article helped you understand WordPress user roles and how to add them to your registration form manually and enroll the user in learndash. Feel free to contact us if you need any help regarding this topic.
Do let us know the topics you want us to cover in the comment section or reach out to us with your queries by clicking the contact form below.