0

I am trying to add a function to my child theme to append the currently logged user as a parameter. For example, when the user test1 logs in and clicks on the following page:

http://www.example.com/my-wishlist

I want her to be redirected automatically to

http://www.example.com/my-wishlist/?user=test1

So far I did the following but it ends in a loop:

$current_user = wp_get_current_user();    
if ( is_user_logged_in() ) {
    wp_redirect( 'http://www.example.com/my-wishlist/?user='.$current_user->user_login );   
    exit;
}

I would appreciate some help. Thank you

  • Did you try javascript redirect? Just put your user name to redirect url with php tag. – bilimokur Mar 11 '18 at 5:51
0

You are getting the redirect loop because your script isn't checking if the user already is redirected.

Try adding another condition to your if to check for the user variable...

$current_user = wp_get_current_user();    
if ( is_user_logged_in() && !isset( $_GET['user'] ) ) {
    wp_redirect( 'http://www.example.com/my-wishlist/?user='.$current_user->user_login );   
    exit;
}
  • Thank you for the fast response. I did what you suggest and seems to add the parameter as expected (with no loop). The problem now, however, is that no matter what link I click on the page it always goes to the page specified in the script (eg. I click home and it still redirects me). Tried this but it does not work. $current_user = wp_get_current_user(); if ( is_user_logged_in() && !isset( $_GET['user'] ) && is_page( 'my-wishlist' ) ) { wp_redirect( 'example.com/my-wishlist/… ); exit; } – ArisF Sep 10 '15 at 10:11
0

Use add_query_arg to make sure the url is created the right way. How is the wishlist link outputted? Trough wp_nav_menu? I would add it to the link on creation, so you don't have to use wp_redirect.

Not the answer you're looking for? Browse other questions tagged or ask your own question.