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:
I want her to be redirected automatically to
/?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( '/?user='.$current_user->user_login );
exit;
}
I would appreciate some help. Thank you
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/my-wishlist
I want her to be redirected automatically to
http://www.example/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/my-wishlist/?user='.$current_user->user_login );
exit;
}
I would appreciate some help. Thank you
Share Improve this question asked Sep 10, 2015 at 8:37 ArisFArisF 11 silver badge2 bronze badges 1- Did you try javascript redirect? Just put your user name to redirect url with php tag. – wpdev Commented Mar 11, 2018 at 5:51
2 Answers
Reset to default 1Use 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.
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/my-wishlist/?user='.$current_user->user_login );
exit;
}