2016-08-08 39 views
1

我們的網站使用Wordpress - WooCommerce登錄頁面供客戶登錄。登錄時使用wp_authenticate()重定向某些用戶

我試圖用wp_authenticate()實現如下:

1)客戶登陸到我們的新網站,輸入自己的用戶名和密碼,並點擊登錄按鈕。如果您想查看WooCommerce登錄文件,請點擊here

2)我們的新網站通過列表查看用戶名是否匹配。如果用戶名匹配,甚至不會查看密碼,只是將用戶重定向到其他網址,如google.com

3)如果用戶名與我們的列表不符,請讓他們照常登錄。

與jQuery,有人幫我想出了下面的代碼:

var names = new Array(」BILL」, 」JIM」, 」BOB」); // get all names into array, and all in uppercase 
var dest_url = 」http://www.website.com」; // URL we want to send them to 
jQuery(document).ready(function() { 
jQuery(」input[name=’login’]」).click(function(event){ 
event.preventDefault(); // prevent default form action 
var current_name = jQuery(」#username」).val(); 
current_name = current_name.trim().toUpperCase(); 
if (-1 != jQuery.inArray(current_name, names)) { 
alert(」Redirecting 」 + current_name + 」 to 」 + dest_url); 
window.location = dest_url; // send to desired URL 
} 
else 
document.getElementsByClassName(」login」)[0].submit(); // input name not on our list, so just do normal submit action 
}); 
}); 

但我不知道,如果wp_authenticate()實際上可以包含內部jQuery腳本。任何建議將不勝感激。

+0

我們分割我們的網站,我們的客戶是老年人,他們不知道他們在做什麼。我們希望通過讓他們像往常一樣登錄到我們的網站儘可能簡單,但這次,我們列表中的應該被重定向到另一個網站。這些人可能不知道他們應該在分手後訪問哪個網站。 – ChrisP777

+0

由於他們生活在特定的國家,名單中的這些人將有資格參加某些保險計劃和教育。所以,他們需要去我們的新網站(我們目前正在建設)我們的舊網站仍然會談論聯邦政府層面的保險。我剛剛提到「谷歌」,因爲我並不想提及正在建設中的新網站的真實網址。我實際上不會將用戶重定向到Google以外的地方。如果您有更多問題,請告訴我。謝謝。 – ChrisP777

回答

1

首先,我會建議在PHP中這樣做,而不是JavaScript。

其次,您有幾個選擇,利用WordPress的內置功能。

如果所有你關心的是用戶名,並不在乎,如果他們成功登錄使用正確的密碼,那麼你可以利用在wp_authenticate()

// This is the filter wp_authenticate fires 
apply_filters('authenticate', null, $username, $password); 

發現過濾知道的是,你可以寫一個快速的小插件,或者將此代碼添加到您的主題的functions.php文件:

// Run this filter with priority 9999 (last, or close to last), after core WP filters have run 
add_filter('authenticate', 'redirect_certain_users', 9999, 3); 

// Your custom filter function 
function redirect_certain_users($user, $username, $password) { 
    // Assumes you write a function called get_list_of_users_to_redirect that returns an array similar to that in your sample code 
    $redirect_users = get_list_of_users_to_redirect(); 

    // If the user is in the array of users to redirect, then send them away 
    if (in_array($username, $redirect_users)) { 
     header("location:http://www.example.com"); 
     die(); 
    } 

    return $user; 
} 

請注意,此代碼是未經測試,但應該讓你至少90%的方式出現。

+0

非常感謝您的回答! ()){$ mylist = array(「jim49」,「amyjones」,「james_ab」,「insurcorp」); return $ mylist;}將工作得到用戶列表?或者我可以簡單地替換$ redirect_users = get_list_of_users_to_redirect();與一個未索引的數組,例如$ redirect_users = array(「jim49」,「amyjones」,「james_ab」,「insurcorp」); 對不起,我是新來的php。 – ChrisP777

+0

我剛剛使用unindexed數組,通過在位於我的子主題文件夾的functions.php文件中編寫以下代碼來簡化它。add_filter('authenticate','redirect_certain_users',9999,3); 函數redirect_certain_users($ user,$ username,$ password){ $ redirect_users = array(「jim49」);如果(in_array($ username,$ redirect)){ header(「location:http://www.example.com」); die(); } return $ user; }雖然,我認爲它不認識陣列。我輸入jim49作爲登錄用戶名和密碼,但仍然說無效的用戶名。 – ChrisP777

+0

我不知道我是否做錯了嗯... – ChrisP777