doulang2311
2019-01-27 02:21
浏览 46
已采纳

WordPress:如何每次将特定用户重定向到特定页面? 这与登录后无关

I use WordPress.

I know how to redirect a user to a specific page after logging in. But let's say they have already logged-in and they click specific URL, they should be redirected to another.

Sample: usernameA The user has already logged-in. He clicks domainname.com/my-account He will be redirected to domainname.com/pageA

usernameB The user has already logged-in. He clicks domainname.com/my-account He will be redirected to domainname.com/pageB

All of them click the domainname.com/my-account but they have to be redirected to a different page.

This redirection is per user. Is there a way to do this in function.php?

图片转代码服务由CSDN问答提供 功能建议

我使用WordPress。

我知道如何将用户重定向到 登录后的特定页面。但是假设他们已经登录并且他们点击了特定的URL,他们应该被重定向到另一个。

示例: usernameA用户已经登录 。 他点击domainname.com/my-account他将被重定向到domainname.com/pageA

usernameB用户已经登录。 他点击domainname.com/my-account他将被重定向到domainname.com/pageB

nn

他们中的所有人都会点击domainname.com/my-account但他们必须重定向到 不同的页面。

此重定向是每个用户。 有没有办法在function.php中执行此操作?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglu3243 2019-01-27 02:54
    已采纳

    You can use the built-in wp_redirect() function. Use a hook where there has been no output yet but wordpress already has the necessary info, like the requested post_id. you can stick something like this in your functions.php

    function myredirect() {
       $page_id = get_queried_object_id();
    
       //im working with the id's here, but for better readability you can also work with slugs. 
       //you shouldnt use the whole url, because it will mean trouble when changing/moving domain-names
       //
       //check if it's the page you want to redirect from.
       if($page_id==138) {
           $user_id=get_current_user_id();
           $redirect_to='';
           switch($user_id) {
               case 1:
                   $redirect_to=54840;
                   break;
           }
           if(!empty($redirect_to)) {
               wp_redirect(get_permalink($redirect_to));
               exit;
           }
       }
    }
    add_action('template_redirect', 'myredirect');
    
    打赏 评论

相关推荐 更多相似问题