登录时使用wp_authenticate()重定向某些用户 [英] using wp_authenticate() to redirect certain users when logging in

查看:128
本文介绍了登录时使用wp_authenticate()重定向某些用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试使用 wp_authenticate()来实现以下目标:

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

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脚本.任何建议将不胜感激.

解决方案

首先,我建议使用PHP而不是javascript.

第二,利用WordPress的内置功能,您有两个选择.

如果您只关心用户名,而不关心他们是否使用正确的密码成功登录,则可以利用

请注意,此代码未经测试,但应该可以让您至少获得90%的访问权限.

Our website is using Wordpress - WooCommerce login page for customers to login.

I am trying to use wp_authenticate() to achieve the following:

1) Customer login to our new website, enter their username and password, and hit Login button. In case you want to see WooCommerce Login file, click here.

2) Our new website goes through list see if the username matches. If the username matches, don't even look at the password, just redirect the user to other url such as google.com

3) if the username doesn't match with our list, just let them login as usual.

With JQuery, someone helped me to come up with the following code:

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

But I am not sure if wp_authenticate() can actually contain jquery script inside. Any suggestion would be greatly appreciated.

解决方案

First, I would recommend doing this in PHP, not javascript.

Second, you have a couple of options, leveraging the built-in functionality of WordPress.

If all you care about is the username, and do not care if they successfully logged in with the right password, then you could leverage the filter found in wp_authenticate()

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

Knowing that, you could write a quick little plugin, or add this code to your theme's functions.php file:

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

Note that this code is untested, but should get you at least 90% of the way there.

这篇关于登录时使用wp_authenticate()重定向某些用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆