登录时使用wp_authenticate()重定向某些用户

我们的网站正在使用Wordpress – WooCommerce登录页面供客户登录。

我试图使用wp_authenticate()来实现以下function:

1)客户登录我们的新网站,输入他们的用户名和密码,然后点击登录按钮。 如果您想查看WooCommerce登录文件,请单击此处 。

2)我们的新网站通过列表查看用户名是否匹配。 如果用户名匹配,请不要查看密码,只需将用户重定向到其他url,例如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脚本。 任何建议将不胜感激。

首先,我建议在PHP中执行此操作,而不是javascript。

其次,你有两个选择,利用WordPress的内置function。

如果您关心的只是用户名,并且不关心他们是否使用正确的密码成功登录,那么您可以利用wp_authenticate()中找到的filter

 // 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%的代码。