登录后重定向到仪表板

时间:2016-06-16 作者:Christopher

我使用以下代码将用户登录时重定向到仪表板,而不是他们的个人资料。

它工作正常,但也会在出现以下情况时重定向到仪表板redirect_to= 在url中http://example.com/wp-login.php?redirect_to=

如果有redirect_to= 在url中重定向到附加的url而不是仪表板?

// Redirect to dashboard on login (instead of profile)
add_filter( \'login_redirect\', \'app_login_redirect\', 10, 3 );
function app_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( \'administrator\', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return admin_url( \'index.php\' );
        }
    } else {
        return $redirect_to;
    }
}

1 个回复
最合适的回答,由SO网友:birgire 整理而成

第二个输入参数$request, 在您的app_login_redirect() 回调函数,从source

isset( $_REQUEST[\'redirect_to\'] ) ? $_REQUEST[\'redirect_to\'] : \'\';
如果我理解正确,你可以检查它是否为空,如果你需要在你的逻辑部分中知道的话。

相关推荐

Redirect htaccess

我需要帮助重定向我的页面。例如,我有网站https://example.com. 我需要将内容从https://example.com 到https://example.com/blog. 这不是问题,我通过更改主页URL来做到这一点。这很有效。但现在我需要添加来自旧的重定向https://example.com 到https://xmpl.com.我想用。htaccess,但这不起作用。你们能帮帮我吗?以下是我对此的一些尝试,但两者都不起作用。RewriteEngine On RewriteRu