如何在验证错误时重新加载角色特定注册表?

时间:2013-08-28 作者:Iurie

我测试解决方案separate registration for different roles 而且效果很好。但是错误验证部分/功能(见下文)重新加载/重用通用注册链接,而不是特定于角色的链接,例如:。http://example.com/wp-login.php?action=register 而不是http://example.com/wp-login.php?action=register&role=seller. 如何避免这种情况?

验证功能:

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

1 个回复
SO网友:Bainternet

您可以使用用户会话或Cookie为用户设置所需角色,例如:

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

结束

相关推荐

Comment form validation

如何设置注释字段的验证规则?我更改了评论者姓名/电子邮件/主页onmouseover和onblur的值(我使用它而不是标签-因此如果字段为空,它会显示“您的电子邮件”、“您的主页”等)。问题是,在提交时,它会在主页字段中提交此文本(因为它没有验证,而不像电子邮件字段,如果您输入了除[email protected]).如何验证主页字段?