首次登录wordpress后重定向用户? [英] Redirect user after first login in wordpress?

查看:38
本文介绍了首次登录wordpress后重定向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码检查用户是否是第一次登录,即注册后.如果是这样,我想将他重定向到自定义页面.否则,将他重定向到主页或管理页面.

This code checks if a user is logging in for the first time, that is after registration. I want to redirect him to a custom page if so. Otherwise, redirect him to the homepage or admin page.

 function mylogin_redirect() {
    global $user_ID;
    if( $user_ID ) {
        $user_info = get_userdata( $user_ID ); 
        // If user_registered date/time is less than 48hrs from now
        // Message will show for 48hrs after registration
        if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
            header("Location: http://example.com/custompage");
        } elseif( current_user_can( 'manage_options' )) {
            header("Location: http://example.com/wp-admin/");
        } else {
            header("Location: http://example.com/");
        }
    }
}
add_action('wp_head', 'mylogin_redirect');

但它不起作用?我的猜测是它不会被 wp_head 钩住......我使用 login_redirect 过滤器尝试了以下操作:

But it doesn't work? My guess is it doesn't get hooked into wp_head... I tried the following using login_redirect filter:

 function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
    global $user_ID;
    if( $user_ID ) {
        $user_info = get_userdata( $user_ID ); 
        // If user_registered date/time is less than 48hrs from now
        // Message will show for 48hrs after registration
        if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
            return get_bloginfo('url') . "/custompage/";
        } elseif( current_user_can( 'manage_options' )) {
            return admin_url();
        } else {
            return get_bloginfo('url');
        }
    }
}
add_filter('login_redirect', 'mylogin_redirect');

虽然它让我登录,但它并没有让我到任何地方,只能使用一个空白页面http://example.com/wp-login.php.

Though it logs me in, it doesn't get me anywhere but to http://example.com/wp-login.php instead with a blank page.

更新:好吧,我不知道发生了什么.使用过滤器钩子,我只能在第二次登录后才能到达预期的目的地.好吧,不是真正的第二次登录,而是第二次单击登录按钮.我是这样做的:输入凭据 -> 登录 ->(错误页面) -> 回击按钮 -> 再次输入凭据 -> 登录 ->(正确页面).奇怪.

UPDATE: Ok, I don't know what's happening. Using the filter hook, I can get to the intended destination only after second login. Well not really second login but on the second click of the login button. I did it like so: enter credentials -> login -> (wrong page) -> hit back button -> enter credentials again -> login -> (correct page). Weird.

推荐答案

您需要像这样调整过滤器调用;

You need to adjust your filter call like so;

// filter name, callback, priority, accepted args
add_filter('login_redirect', 'mylogin_redirect', 10, 3);

这篇关于首次登录wordpress后重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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