How to reload the role specific registration form on validation errors?

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();

Leave a Comment