0

I want to show a select box for user roles in the registration form. I am using this model:

/* ROLES IN LIST REGISTRATION */
// 1. Add a new form element...

add_action( 'register_form', 'odin_register_form' );
function odin_register_form() {

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

// 2. Add validation.

add_filter( 'registration_errors', 'odin_registration_errors', 10, 3 );
function odin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if (
    empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == ''
    || $_POST['role'] == 'administrator'
    || $_POST['role'] == 'editor'
    || $_POST['role'] == 'contributor'
    || $_POST['role'] == 'author'
    || $_POST['role'] == 'shop_manager'
    ) {
     $errors->add( 'role_error', __( '<strong>ERROR</strong>: Select a valid option.', 'odin' ) );
}
return $errors;
}

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

if (
    $_POST['role'] != 'administrator'  ||
    $_POST['role'] != 'editor'  ||
    $_POST['role'] != 'contributor'  ||
    $_POST['role'] != 'shop_manager'  ||
    $_POST['role'] != 'author'
) {
    $user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
    }
}

The point is that I have not seen the error happen when validation choose an empty option.

So what I alncançar is as follows:

1) Filter the types of users except the Administrator, Editor, Author, Contributor, and the Store Manager. I could just set the Subscriber and Customer functions, but the project must be free to create other roles without having to write the code again.

2) I need to validate the record of the options in this field, because it already has achieved filter rendering of roles in the selection list, we have to ensure that there is the possibility of u stranger inject an option in html with the value 'administrator' for example. This may not happen for any of the papers with some editing capability.

I need the hint of you how I can do this.

4
  • if you are combining && and || conditions, you will need extra brackets to group the right conditions together eg. ( ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' )... that could be it...
    – majick
    Commented Apr 6, 2016 at 14:59
  • i try empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' but not validate. Nothing happens. Commented Apr 6, 2016 at 15:30
  • no look at the brackets again: empty( $_POST['role'] ) || ( ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' )
    – majick
    Commented Apr 6, 2016 at 16:00
  • No validation. Nothing Happens. Commented Apr 6, 2016 at 17:56

2 Answers 2

0

You can use wp_dropdown_roles()

2
  • I do not know why but I'm getting Fatal error: Call to undefined function wp_dropdown_roles() Commented Apr 6, 2016 at 20:22
  • the file the wp_dropdown_roles function is in is not included by default in non-admin screens. you need to include /wp-admin/includes/templates.php for it to work frontend.
    – majick
    Commented Apr 7, 2016 at 4:11
0

I think !isset is a more reliable way of testing so I added that:

add_filter( 'registration_errors', 'odin_registration_errors', 10, 3 );
function odin_registration_errors( $errors, $sanitized_user_login, $user_email ) {  

    if (
        !isset( $_POST['role'] )
        || empty( $_POST['role'] ) 
        || trim( $_POST['role'] ) == ''
        || $_POST['role'] == 'administrator'
        || $_POST['role'] == 'editor'
        || $_POST['role'] == 'contributor'
        || $_POST['role'] == 'author'
        || $_POST['role'] == 'shop_manager'
     ) {
    $errors->add( 'role_error', __( '<strong>ERROR</strong>: Select a valid role option.', 'odin' ) );
    }

    // debug $error to file
    $debugfile = get_stylesheet_directory().'/roledebug.txt';
    ob_start(); print_r($errors); $debugdata = ob_get_contents();
    file_put_contents($debugfile,$debugdata); ob_end_clean();        

    return $errors;
}             

And I am not sure but maybe change the <select> element selected attribute to selected="selected" in the first function also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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