• 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)
  • Thread Starter priyadilsha

    (@priyadilsha)

    I also tried the below code but no luck….

    <p class="tml-submit-wrap">
                            <label for="select-user-role">Please select your role</label>
                            <select id="select-user-role" name="select-user-role" class="input select">
                                <option value="author">Author</option>
                                <option value="reviewer">Reviewer</option>
                            </select>
    </p>
    function tml_new_user_registered( $user_id ) {
      $u = new WP_User(  $user_id );
    
        if($_REQUEST['select-user-role']=='reviewer') {
          $u->remove_role( 'subscriber' );
          $u->add_role( 'editor' );
        }
    }
    add_action( 'user_register', 'tml_new_user_registered' );
    
    function myplugin_user_register( $user_id ) {
    
       update_user_meta( $user_id, 'role', $_POST['select-user-role'] );
    }
    add_action( 'user_register', 'myplugin_user_register' );

    Thanks

    Plugin Author Jeff Farthing

    (@jfarthing84)

    Either of those should be working. Have you verified that those callbacks are actually being run? Try throwing a die('test'); in there or some other way to verify that the callbacks are actually firing.

    Thread Starter priyadilsha

    (@priyadilsha)

    Hi,

    Thanks so much for the reply. I didn’t get chance to solve this. So, I used another plugin for the site.

    Thanks Again

    Hi Priyadilsha

    I have a similar requirement – namely, using a couple of select dropdowns in registration form. Please let me know which plugin you used to solve the problem. Was it along with “theme my login” or as replacement?

    Please do reply. Thanks & regards.

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.