繁体   English   中英

首次登录wordpress后重定向用户?

[英]Redirect user after first login in wordpress?

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

 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过滤器:

 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而不是空白页面。

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

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

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

在WordPress首次登录时重定向用户 :基于Cookie的解决方案和基于用户元表的解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM