Redirect specific link for logged in users only to specific URL

admin2025-06-06  3

i have a custom Woocommerce link that adds a product to the cart and redirects user to the account page:

account/?add-to-cart=15169

I'd like this link to redirect only logged-in users to some specific URL and all the other users to the Account page

Right now:

Not Logged in users redirect to create an account page, that is fine.

Logged in users redirect to Account page

Question: Is there a way to redirect only logged-in users to a specific url for this specific link rather than the account page?

I have tried:

//redirect logged-in users from Account page id=90 to specific page id=15498
function my_logged_in_redirect() {

if ( is_user_logged_in() && is_page( 90 ) )
{
    wp_redirect( get_permalink( 15498 ) );
    die;
}

}
add_action( 'template_redirect', 'my_logged_in_redirect' );

that works but then the account page is actually not accessible from logged-in users.

i have a custom Woocommerce link that adds a product to the cart and redirects user to the account page:

account/?add-to-cart=15169

I'd like this link to redirect only logged-in users to some specific URL and all the other users to the Account page

Right now:

Not Logged in users redirect to create an account page, that is fine.

Logged in users redirect to Account page

Question: Is there a way to redirect only logged-in users to a specific url for this specific link rather than the account page?

I have tried:

//redirect logged-in users from Account page id=90 to specific page id=15498
function my_logged_in_redirect() {

if ( is_user_logged_in() && is_page( 90 ) )
{
    wp_redirect( get_permalink( 15498 ) );
    die;
}

}
add_action( 'template_redirect', 'my_logged_in_redirect' );

that works but then the account page is actually not accessible from logged-in users.

Share Improve this question edited Nov 27, 2018 at 22:14 xbass540 asked Nov 27, 2018 at 22:09 xbass540xbass540 1336 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

you can check the query string and redirect if query string matches the product id.

function my_logged_in_redirect() {
    if ( is_user_logged_in() && is_page( 90 ) && $_GET['add-to-cart'] == 15169 ) {
        wp_redirect( get_permalink( 15498 ) );
        die;
    }
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749147345a316769.html

最新回复(0)