using wp_authenticate() to redirect certain users when logging in

徘徊边缘 提交于 2019-12-08 05:20:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!