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 test a solution for separate registration for different roles and it works very well. But the validation part/function (see bellow) on errors reloads/reuse the generic registration link and not the role specific link, e.g. http://example.com/wp-login.php?action=register instead of http://example.com/wp-login.php?action=register&role=seller. How to avoid this?

The validation function:

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");
        }
    }
}
share|improve this question
add comment

1 Answer

You can either user session or cookies to setup the user for the desired role e.g:

/**
* register_roles_with_cookies
*/
class register_roles_with_cookies
{

    function __construct($args = array()){
        //create a hidden field for role and extra fields needed
        add_action('register_form',array($this,'add_hidden_role_field'));
        //validate
        add_action('register_post',array($this,'my_user_fields_validation'),10,3);
        //save the role
        add_action('user_register', array($this,'update_role'));
    }

    public function setCookie($name,$val,$time = false){
        if(false === $time)
            $time = time() + (86400 * 7)); // 86400 = 1 day
        setcookie($name,$val,$time);
    }

    public function getCookie($name){
        if (isset($_COOKIE[$name]))
            return $_COOKIE[$name];
        return false;
    }

    public function add_hidden_role_field(){
        $user_type = isset($_GET['role'])? $_GET['role'] : (($this->getCookie('user_role'))? $this->getCookie('user_role'): false);
        if($user_type){
            $this->setCookie('user_role',$user_type);
            echo '<input id="user_role" type="hidden" tabindex="20" size="25" value="'.$user_type.'" 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
        }
    }



    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");
            }
        }
    }



    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']);
            }
       }
    }
}

new register_roles_with_cookies();
share|improve this answer
    
1) Is here an extra ')': $time = time() + (86400 * 7)); // 86400 = 1 day?; 2) And how to use this code? I simply put it in functions.php? But nothing happens! And the extra fields do not appear anymore (I removed the old code). –  Iurie Aug 28 '13 at 14:54
    
I added the new register_roles_with_cookies(); row after the class code and now the extra fields appeared again, but with an error: "Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/dev/wp-login.php:58) in /home/user/public_html/dev/wp-content/themes/designfolio_child/functions.php on line 96". –  Iurie Aug 28 '13 at 16:03
    
... the error appear in the registration form/page. –  Iurie Aug 28 '13 at 16:22
    
I changed the theme, from Designfolio to Twenty Twelve, and the error are the same. The specified line from this class code is setcookie($name,$val,$time); from the public function setCookie() function. I edited the setcookie to setCookie, but the error persist. –  Iurie Aug 28 '13 at 17:14
    
The warning is clear. Warning: Cannot modify header information - headers already sent which means that cookies will not work in this solution so i guess you can work with sessions instead. –  Bainternet Aug 29 '13 at 8:20
add comment

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.