I need to present a choose field for consumer roles within the registration kind. I’m utilizing this mannequin:
/* ROLES IN LIST REGISTRATION */
// 1. Add a brand new kind component...
add_action( 'register_form', 'odin_register_form' );
operate odin_register_form() {
world $wp_roles;
echo '<choose title="position" class="enter">';
echo '<possibility disabled chosen worth> -- </possibility>';
foreach ( $wp_roles->roles as $key=>$worth ):
echo '<possibility worth="'.$key.'">'.$worth['name'].'</possibility>';
endforeach;
echo '</choose>';
}
// 2. Add validation.
add_filter( 'registration_errors', 'odin_registration_errors', 10, 3 );
operate 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'] == 'writer'
|| $_POST['role'] == 'shop_manager'
) {
$errors->add( 'role_error', __( '<robust>ERROR</robust>: Choose a legitimate possibility.', 'odin' ) );
}
return $errors;
}
//3. Lastly, save our further registration consumer meta.
add_action( 'user_register', 'odin_user_register' );
operate odin_user_register( $user_id ) {
if (
$_POST['role'] != 'administrator' ||
$_POST['role'] != 'editor' ||
$_POST['role'] != 'contributor' ||
$_POST['role'] != 'shop_manager' ||
$_POST['role'] != 'writer'
) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'position' => $_POST['role'] ) );
}
}
The purpose is that I’ve not seen the error occur when validation select an empty possibility.
So what I alncançar is as follows:
1) Filter the sorts of customers besides the Administrator, Editor, Writer, Contributor, and the Retailer Supervisor. I may simply set the Subscriber and Buyer features, however the challenge have to be free to create different roles with out having to put in writing the code once more.
2) I must validate the report of the choices on this discipline, as a result of it already has achieved filter rendering of roles within the choice listing, we’ve to make sure that there may be the potential for u stranger inject an possibility in html with the worth ‘administrator’ for instance. This may occasionally not occur for any of the papers with some modifying functionality.
I want the trace of you the way I can do that.