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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效