Allow user to select the role during the registration
-
Hi,
Thanks for the nice and useful plugin. I need to allow user to select their role (subscriber or editor) during the registration. I’m using the below code to accomplish this task but it’s not working. Can someone tell me what’s the issue is ? I tried but can’t figure it out.
/1. Add a new form element... add_action( 'register_form', 'myplugin_register_form' ); function myplugin_register_form() { global $wp_roles; echo '<p class="tml-submit-wrap">'; echo '<label for="select-user-role">Please select your role</label>'; echo '<select name="role" class="input select select-user-role">'; foreach ( $wp_roles->roles as $key=>$value ) { if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', 'Pending', 'Author' ] )) { echo '<option value="'.$key.'">'.$value['name'].'</option>'; } } echo '</select>'; echo '</p>'; } //2. Add validation. add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 ); function myplugin_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>: You must include a role.' ) ); } return $errors; } //Save extra registration user meta. add_action( 'user_register', 'myplugin_user_register' ); function myplugin_user_register( $user_id ) { wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) ); }
Any help will much appreciated.
Thanks
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Allow user to select the role during the registration’ is closed to new replies.