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've set 2 new roles for my site using the "Members" plugin - "sellers" and "buyers".

Each role should have its own registration page and login. A visitor can register as both a buyer and a seller but he can only be logged in as one of them. So, if a visitor has logged in as a seller and then goes to the buyer page he'll be able to login as a buyer and when he does so he will automatically be logged out as a seller.

I've searched for hours for a plugin that will give me the option to do this but couldn't find one. Sounds like a pretty common configuration in websites with different role registrations.

Does a plugin such as this even exist? And if not how can this be done?

share|improve this question
1  
you don't need separate forms, just a single register form with a means of selecting role. once they are registered to a role, everyone can use the same login page. if someone registers as a buyer, there's no way they can log in as anything but buyer with their credentials. maybe something like this will point you in the right direction. –  Milo Apr 29 '11 at 16:13
add comment

1 Answer 1

up vote 6 down vote accepted

Creating two separate registration for different roles is easy:

//create a hidden field for role
add_action('register_form','add_hidden_role_field');

function add_hidden_role_field(){
    if (isset($_GET['role'])){
        echo '<input id="user_email" type="hidden" tabindex="20" size="25" value="'.$_GET['role'].'" name="role"/>';
    }
}

add_action('user_register', 'register_role');

//save the the role
function update_role($user_id, $password="", $meta=array()) {
   if (isset($_POST['role'])){
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = $_POST['role'];

       //only allow if user role is my_role to avoid a few new admins to the site
       if (($userdata['role'] == "seller") or ($userdata['role'] == "buyer")){
          wp_update_user($userdata);
       }
   }
}

and now you can link each role with "its own" registration form:

seller: http://example.com/wp-login.php?action=register&role=seller
buyer: http://example.com/wp-login.php?action=register&role=buyer

but as Milo commented:

"if someone registers as a buyer, there's no way they can log in as anything but buyer with their credentials"

which means that they would have to use a different email to register the other role.

Update

this is an update with an example to show how you can use the same fore but with different fields for each role.

So you just need to change the functions a bit:

//create a hidden field for role and extra fields needed
add_action('register_form','add_hidden_role_field');

function add_hidden_role_field(){
    if (isset($_GET['role'])){
        $user_type = $_GET['role'];
        echo '<input id="user_email" type="hidden" tabindex="20" size="25" value="'.$_GET['role'].'" name="role"/>';
    }
    if (isset($user_type) && $user_type == "seller"){
        //add extra seller fields here eg:
        ?>
        business name:
        <input id="user_email" type="text" tabindex="20" size="25" value="" name="business_name"/>

        business address:
        <input id="user_email" type="text" tabindex="20" size="25" value="" name="business_address"/>
        <?php
    }
    if (isset($user_type) && $user_type == "buyer"){
        //add extra buyer fields here eg:
        ?>
        buyer name:
        <input id="user_email" type="text" tabindex="20" size="25" value="" name="buyer_name"/>
        <?php
    }
}

this way only the fields needed by the specific role are shown.

Next is if you want to have some kind of validation to these extra fields you can use register_post hook for example:

add_action('register_post','my_user_fields_validation',10,3);

function my_user_fields_validation($login, $email, $errors) {
    global $firstname, $lastname;
    //get the role to check
    if (isset($_POST['role'])){
        $user_type = $_POST['role'];
    }
    //check the fields according to the role
    if (isset($user_type) && $user_type == "seller"){
    //check sellers fields
        if ($_POST['business_name'] == '') {
            $errors->add('empty_business_name', "<strong>ERROR</strong>: Please Enter in a Business name");
        }
        if ($_POST['business_address'] == '') {
            $errors->add('empty_business_address', "<strong>ERROR</strong>: Please Enter in Business address");
        } 
    }
    if (isset($user_type) && $user_type == "buyer"){
        //check buyers fields
        if ($_POST['buyer_name'] == '') {
            $errors->add('empty_buyer_name', "<strong>ERROR</strong>: Please Enter in a Buyer name");
        }
    }
}

then if every thing is fine just save the fields in the user meta based on the role

add_action('user_register', 'update_role');

//save the role
function update_role($user_id, $password="", $meta=array()) {
   if (isset($_POST['role'])){
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = $_POST['role'];
        $user_type = $_POST['role'];
       //only allow if user role is my_role to avoid a few new admins to the site
        if (($userdata['role'] == "seller") or ($userdata['role'] == "buyer")){
            wp_update_user($userdata);
        }
        if (isset($user_type) && $user_type == "seller"){
            //save sellers fields
            update_user_meta($user_id, 'business_name', $_POST['business_name']);
            update_user_meta($user_id, 'business_address', $_POST['business_address']);
        }
        if (isset($user_type) && $user_type == "buyer"){
            //save sellers fields
            update_user_meta($user_id, 'buyer_name', $_POST['buyer_name']);
        }
   }
}
share|improve this answer
    
Thank you Bainternet & Milo! Yes, I mean the visitor will register twice - once as a seller and another as a buyer, with different emails. But I do need two separate registration forms since each of them will contain different fields. For example: the seller's reg form will include the fields - "business name" and "business address" while the buyer's reg form will include the field "buyer name". So, Bainternet, I'm not sure I can use your code as it is (but thanks a lot for the effort) since it points to the same page only with different parameters. How can I do it with two separate forms? –  Ash May 1 '11 at 10:51
    
@Ash: you still don't need two separate forms, just add the fields based on the role, i'll updated my answer with an example. –  Bainternet May 1 '11 at 14:01
    
@Ash: check the update in the answer. –  Bainternet May 1 '11 at 14:24
    
@Bainternet, Thanks a lot for the additional code! –  Ash May 2 '11 at 9:10
    
@Bainternet: Sorry for the newbie question but that's what I am :-) - is this code only usable in a plugin or can it also be used in a php file which is a template for a page? –  Ash May 2 '11 at 9:13
show 5 more comments

protected by toscho Mar 14 '13 at 16:56

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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