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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.