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?

  • 写回答

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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程