dongwu9972 2019-03-08 08:57
浏览 66
已采纳

WooCommerce - 根据用户的角色重定向登录用户

This is a query based around WooCommerce Membership and Subscriptions.

I must add that I'm also trying to decide if it's good UX to do what I'm doing.

There's many solutions to redirecting users after they login but I have a situation where I want to redirect a user with a role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member. So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.

Again there are various roles and redirect plugins but none seem to help in this specific scenario. So the source of the code I've used is here: SOURCE and I'm looking to do something like this:

function eks_redirect_users_by_role() {

    global $post;
    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];


    if ( 'subscriber' === $role_name && $post->ID == 47145)  {
        wp_redirect( '/my-account' );
    } 

} 
add_action( 'admin_init', 'eks_redirect_users_by_rol
e' );

So if the user role is subscriber and they try and visit the page idea it's redirected.

At the current time it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.

  • 写回答

2条回答 默认 最新

  • duannai5879 2019-03-09 10:00
    关注

    I was able to achieve want I wanted to the following way:

    function eks_redirect_user() {
    
        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];
        $postid = get_the_ID();
    
        if ( 'subscriber' === $role_name && $postid == 47145  ) { ?>
            <script>
    function redirectPage() {
    
        window.location = "/my-account/";
    }
    
    redirectPage();
    </script>
    
    <?php
            exit;
        }
    
    }
    add_action('wp_footer', 'eks_redirect_user' );
    

    The issue is it's a fairly untidy solution as the redirect feels odd. I'm not sure if using wp_redirect would work better. I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站