This is my first ever question here, hope I'm doing it all right. I'm using this Tutorial for trying to add a radio-button for setting the user-role to the standard Wordpress register form.
Unfortunately my code does not work properly ($role won't even be filled and I keep getting my own error message). My php knowledge is still at the beginning.
...
if($_POST['roleType'] == 'eins') {
$role = '1';
} elseif($_POST['roleType'] == 'zwei') {
$role = '2';
} elseif($_POST['roleType'] == 'drei') {
$role = '3';
}
// else {
// $role = 'subscriber';
// }
?>
<p>
<label><?php esc_html_e( 'Was wollen Sie inserieren', 'crf' ) ?><br/>
<input type="radio"
name="roleType"
value="eins"
/>1<br>
<input type="radio"
name="roleType"
value="zwei"
/>2<br>
<input type="radio"
name="roleType"
value="drei"
/>3<br>
</label>
</p>
<?php
}
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors ) {
if ( empty( $role ) ) {
$errors->add( 'roleType_empty', __( '<strong>ERROR</strong>: Please choose what content you are going to create.', 'crf' ) );
}
return $errors;
}
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
if ( ! empty( $role ) ) {
update_user_meta( $user_id, 'role', $role );
}
}
I would be greatful if anyone could give me a short hint on what I'm doing wrong, here.
Thanks an cheers, Johannes
Welcome to Stack Overflow.
In the crf_user_register
function, $role
is empty because that variable doesn't have a value within the function's scope.
You can either assign a value to $role
:
function crf_user_register( $user_id ) {
$role = $_POST['roleType'];
if ( ! empty( $role ) ) {
update_user_meta( $user_id, 'role', $role );
}
}
Or just use the POST variable:
function crf_user_register( $user_id ) {
if ( ! empty( $_POST['roleType'] ) ) {
update_user_meta( $user_id, 'role', $_POST['roleType'] );
}
}