问题描述

我使用”Members” 插件 – “sellers” 和”buyers” 为我的网站设置了 2 个新角色。

每个角色都应该有自己的注册页面和登录名。访客可以注册为买方和卖方,但他只能以其中之一登录。所以,如果访问者以卖家身份登录,然后进入买家页面,他将能够以买方身份登录,而当他这样做时,他将自动作为卖家注销。

我搜索了几个小时,一个插件,将给我选择这样做,但找不到一个。听起来像一个非常普遍的配置在不同的角色注册的网站。

这样的插件甚至存在吗?如果不是这样做呢?

最佳解决方案

为不同角色创建两个单独的注册很容易:

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

现在您可以链接每个角色与”its own” 注册表单:

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

但正如米洛所说:

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

这意味着他们必须使用不同的电子邮件来注册其他角色。

Update

这是一个更新示例,以显示如何使用相同的前缀,但每个角色使用不同的字段。

所以你只需要改变功能:

//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
    }
}

这样只显示了特定角色所需的字段。

接下来是如果你想对这些额外的字段进行某种验证,可以使用 register_post 钩子来做例子:

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

那么如果每件事情都很好,只需将该字段保存在基于角色的用户元数据中

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

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。