I have a classipress site and want to have the user choose upon registering with the site if they are a teacher, or student. These will be different roles. I have been trying to get roles at registration plugin to work because it is basically what I need, but since classipress has a custom reg page it does not seem to work. Any ideas?
1 Answer
In your functions.php file do this:
add_action('register_form','register_role_field');
add_action('register_post','check_fields',10,3);
add_action('user_register', 'register_role_fieldforuser');
// This will register new field in registration form
function register_role_field(){ ?>
<label>Choose your role:<br/>
<!--- Let's check if there role already set. If $_GET['role'] = 0 -then this is teacher if 1 = student --->
<?php if ( isset( $_GET['role'] ) ) { ?>
<select id="role" class="checkboxas" name="role">
<option value="0" <?php if( $_GET['role'] == '0' ) echo 'selected="selected"';?>>Teacher</option>
<option value="1" <?php if( $_GET['role'] == '1' ) echo 'selected="selected"';?>>Student</option>
<?php } else { ?>
<!--- Incase we don't get role ---->
<select id="role" class="checkboxas" name="role">
<option value="0">Teacher</option>
<option value="1" selected="selected">Student</option>
<?php } ?>
</select>
</label>
<?php }
// Here we will check fields in this case only one
function check_fields( $login, $email, $errors ) {
if( !is_numeric( $_POST['role'] ) )
$errors->add( 'bad_role', "ERROR: You have to select role" );
}
function register_role_fieldforuser( $user_id, $password="", $meta=array() ) {
if( is_numeric( $_POST['role'] ) ) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['first_name'] = $_POST['first'];
$userdata['last_name'] = $_POST['last'];
if( $_POST['role'] == 0 )
$userdata['role'] = 'teacher';
else $userdata['role'] = 'student';
wp_update_user($userdata);
}
}
You can make 2 links in your website:
1 for teachers: http://yourwebsite.com/wp-login.php?action=register&role=0
and another for students
http://yourwebsite.com/wp-login.php?action=register&role=1
So registration form automaticaly will select user role based on role set in uer.