問題描述
我使用”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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。