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  ) { ?>
    function redirectPage() {
        window.location = "/my-account/";
    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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题