Adding radio choices and updating the role for a new registered user
-
I am trying to implement a register form where I have a role selection radio button with choices”Role1″ & “Role2” and other form elements
It doesn’t update the role for the registered user. It just adds None to the role after the registration is complete, but in essence I need the role which was selected to be saved when saving the username and other fields.
Here is what I have so far –
I have a registration form with fields which has radio choices like –
<label> <input type="radio" name="user_role" id="Role1<?php $template->the_instance(); ?>_r" class="input" <?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role1' ) echo 'checked="checked"'; ?> value="Role1"/>Role1 </label> <label> <input type="radio" name="user_role" id="Role2<?php $template->the_instance(); ?>" class="input" <?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role2' ) echo 'checked="checked"'; ?> value="Role2"/>Role2 </label>
This is what I have in another page that will process the incoming data.
The error module works fine, ie. It only shows the error message when I am not checking any role in my registration page.<?php function tml_registration_errors( $errors ) { if ( !isset( $_POST['user_role'] ) ) $errors->add('empty_user_role', '<strong>ERROR</strong>: Please select a role.'); return $errors; } add_filter( 'registration_errors', 'tml_registration_errors' );
Saving module below doesn’t take effect and I am having issues. For debugging purpose I have the alert setup but that doesn’t alert show the alert box either, it is just bypassed and “None” is added to the role column in the dashboard view of user list.
function tml_user_register( $user_id ) { $user = new WP_User( $user_id ); if ($_POST['user_role'] = 'Role1'){ echo "<script>alert('" . $_POST['user_role'] . "');</script>"; $user->remove_role( 'Default Role' ); $user->set_role( 'Role1' ); update_user_meta( $user_id, 'role', $_POST['user_role'] ); unset( $user ); }else{ echo "<script>alert('" . $_POST['user_role'] . "');</script>"; $user->remove_role( 'Default Role' ); $user->add_role( 'Role1' ); $user->set_role( $_POST['user_role'] ) ; update_user_meta( $user_id, 'role', $_POST['user_role'] ); unset( $user ); } } add_action( 'user_register', 'tml_user_register' ); ?>
Please help me with this, I am trying for past one or two days but in vain. I am not a hardcore PHP programmer but very much a beginner, any help is really appreciated. Thank you in advance.
- The topic ‘Adding radio choices and updating the role for a new registered user’ is closed to new replies.