PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, September 3, 2022

[FIXED] How To Redirect Logged In User To My Account If They Visit To Login & Signup Page?

 September 03, 2022     authentication, woocommerce, wordpress     No comments   

Issue

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://mybusinessurl.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?


Solution

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')) {


Answered By - Michal Trojanowski
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing