Home > database >  How To Redirect Logged In User To My Account If They Visit To Login & Signup Page?
How To Redirect Logged In User To My Account If They Visit To Login & Signup Page?

Time:08-14

I want to set the redirection for the login & signup page to my account page. If a user is already logged in & they have a customer role & tries to open the login & signup page they should be redirected to my account page. I'm using this code.

add_action( 'template_redirect', 'redirect_to_myaccount_page' );
function redirect_to_myaccount_page() {
    if ( is_page('login') || is_page('signup') && is_user_logged_in() && wc_user_has_role( $user, 'customer')) {
        wp_redirect( 'https://pixelnthings.com/my-account', 301 ); 
        exit;
    }
}

But the problem is when I'm trying to access the URL (mydomain.com/login or mydomain.com/signup) as a new user it's redirecting to a my-account page. and gives me an error [ERR_TOO_MANY_REDIRECTS]. Please let me know how can I fix this?

CodePudding user response:

Your OR condition need to be in parenthesis. Currently you check for "is login page OR the three other condition". That's why you always redirect to my account page when going to login.

This should do the trick:

if ( (is_page('login') || is_page('signup')) && is_user_logged_in() && wc_user_has_role( $user, 'customer')) {
  • Related