1

How to add users roles dropdown in registration in wordpress

 function myplugin_register_form() {

global $wp_roles;

echo '<select name="role">';
foreach ( $wp_roles->roles as $key=>$value ): 
echo '<option value="'.$key.'">'.$value['name'].'</option>';
 endforeach;
echo '</select>';


}

add_action( 'register_form', 'myplugin_register_form' );

add_action( 'user_register', 'myplugin_user_register' );
    function myplugin_user_register( $user_id ) {
        if ( ! empty( $_POST['role'] ) ) {
            update_user_meta( $user_id, 'role', trim( $_POST['role'] ) );
        }
    }

i used this code in functions.php but not working regards

5
  1. https://codex.wordpress.org/Customizing_the_Registration_Form
  2. https://codex.wordpress.org/Function_Reference/wp_update_user

Instead of update_user_meta function in user_register hook use wp_update_user

Following is the working example:

//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_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'], [ 'Administrator', 'Contributor', ] ) {
          echo '<option value="'.$key.'">'.$value['name'].'</option>';
       }
    }
    echo '</select>';
}

//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.', 'mydomain' ) );
    }

    return $errors;
}

//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {

   $user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}
-2
<?php global $wp_roles; ?>

<select name="role">
<?php foreach ( $wp_roles->roles as $key=>$value ): ?>
<option value="<?php echo $key; ?>"><?php echo $value['name']; ?></option>
<?php endforeach; ?>
</select>
  • how to add roles filter to default wordpress registration form? – madu Jul 26 '15 at 7:50
  • go through it. codex.wordpress.org/Customizing_the_Registration_Form – sohan Jul 26 '15 at 7:54
  • if add filter filters not working if i select editor its not saving like editor its saving with Default Role ie subscriber in regsitration – madu Jul 26 '15 at 8:06
  • use filter in your themes functions.php. if this don't work try to assign higher priority. – sohan Jul 26 '15 at 8:14
  • 1
    Please add a proper explanation to your code. Code only answers is low quality as I have explaned many times. Such answers deserves a downvote as it is actually useless to future users – Pieter Goosen Jul 26 '15 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.