Leave a Reply

1 comment

  1. 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', 'update_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']);
            }
       }
    }