简体   繁体   中英

Homepage url redirect when logged in users type it in

我希望我的网站的用户在登录时被重定向。例如:注销用户的主页是 www.test.com,当他们登录时,我希望他们在访问 www 时被重定向到不同的 URL。 test.com,有什么办法可以用一些代码来实现吗?

Solution is using login_redirect hook if you want to set redirect url role wise then use below code please.

Open theme functions.php file Copy and paste the following code: Please modify code as per your role wise.

function rl_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

        if ( in_array( 'author', $user->roles ) ) {
            // enter url which you link redirect for author role users. default is home page.
            return home_url();
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
} 
add_filter( 'login_redirect', 'rl_login_redirect', 10, 3 );

if you want to set redirect url for all users then use below code please.

function rl_user_default_page() {
  return '/new-page-url';
}

add_filter('login_redirect', 'rl_user_default_page');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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