Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
are you working in a child theme? probably the best will be to modify the registration template... –  brasofilo Mar 27 '12 at 0:17
    
hi can you post your register form and register process function that classipress uses by delfault –  fritids May 5 '12 at 22:25

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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