最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

urls - Use wp_redirect to add a parameter

programmeradmin0浏览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:


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
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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;
}
发布评论

评论列表(0)

  1. 暂无评论