Posted on

Redirect after logging in based on the user role

Not every user should be able to log in to the Dashboard, and WooCommerce already prevents this. But you may want to change it for somebody or just use a different page.

By default WooCommerce redirects the user to the My Account page after a successful login.

You can redirect after logging in and use a custom URL based on the user role, like the Dashboard for admins and My Account page for customers.

Add this code at the end of the file functions.php in wp-content/themes/your-child-theme-name/:

Delete the sign <?php on first line if you are having errors come up after saving the file.

If you need to change the URL for a specific role you can use the function get_permalink() passing the page ID to return its URL:

get_permalink( 150 );

This code will return the URL to the page with ID 150.

19 thoughts on “Redirect after logging in based on the user role

  1. Hi Nicola,

    Thank you for the script. But I have a question.

    How to keep any user on checkout page if he is using the login form located on checkout page?

    Thank you,

    1. Hi,
      You can check that with is_checkout() and eventually change the URL used.

      1. Hi Nicola, thank you.

        But I tried with is_checkout() and did not work. Then I double checked with Woo support and had this response:

        The is_checkout condition isn’t going to work here, because they won’t currently be on that page. This is the function you will find the most use in: https://codex.wordpress.org/Function_Reference/wp_get_referer

        The last redirect uses it: http://cld.wthms.co/185cI/4PwqcWVV. You will need to find out what that returns when they are coming from the checkout page, then you can set up a condition and send them back to the checkout page.

        Is it the correct?

        1. Oh yeah, right.

          Then you can check if the referer is equal to the checkout page URL and apply the change.

  2. hi! so i have used your sample here as a template to solve my login redirect issues. i made changes as needed and i was hoping you could peak at my work for any errors i may not have noticed? i am just learning wordpress and php so this is a little new for me.

    my goal is to have wholesale customers redirect to a separate page. here is what i did:

    <?php
    /**
     * Redirect users to custom URL based on their role after login
     *
     * @param string $redirect
     * @param object $user
     * @return string
     */
    function wc_custom_user_redirect( $redirect, $user ) {
        // Get the first of all the roles assigned to the user
        $role = $user->roles[0];
    
        $dashboard = admin_url();
        $myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
        $myaccountwsale = get_permalink( 7956 );
    
        if( $role == 'administrator' ) {
            //Redirect administrators to the dashboard
            $redirect = $dashboard;
        } elseif ( $role == ‘wholesale’ || $role == ‘wholesale_customer’ ) {
            //Redirect wholesale customers to the "My Wholesale Account" page
            $redirect = $myaccountwsale;
        } else {
            //Redirect any other role to the previous visited page or, if not available, to the home
            $redirect = wp_get_referer() ? wp_get_referer() : home_url();
        }
    
        return $redirect;
    }
    add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
    

    thank you in advance!
    jamie

    1. Hi Jamie,
      The code looks good to me!

      1. thank you so much!!! i really appreciate your reviewing this.

  3. Hello sir,

    how to redirect, payment gateway page cancelled redirect to cart page, cart details shown not empty cart. can you help me please

    1. I’m sorry but I’m not sure to understand what you mean.

      Can you please explain?

  4. Hi Nicola,
    Thanks for sharing. This is exactly what I was searching for and it worked perfectly.
    B.Rgds,
    Gabriele

    1. Redirect to previous page after login:

      By the way, I wonder why the wp_get_referer() didn’t work.
      After searching a lit bit more, I found out that I should use $_SERVER[‘HTTP_REFERER’] in place of wp_get_referer() and it worked fine for me.

      So, for anyone else who runs into this problem, use $_SERVER[‘HTTP_REFERER’] in place of wp_get_referer(). I’m not sure why but it works when the other sometimes doesn’t. Hopefully this saves someone else the same hours of trial and errors. 😉

      This is the code after my adjustments:

      function wc_custom_user_redirect( $redirect, $user ) {
      // Get the first of all the roles assigned to the user
      $role = $user->roles[0];
      $dashboard = admin_url();
      $myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
      $home = home_url();
      $previoupage = $_SERVER[‘HTTP_REFERER’];
      if( $role == ‘administrator’ ) {
      //Redirect administrators to the dashboard
      $redirect = $dashboard;
      } elseif ( $role == ‘shop-manager’ ) {
      //Redirect shop managers to the homepage
      $redirect = $home;
      } elseif ( $role == ‘editor’ ) {
      //Redirect editors to the previous page
      $redirect = $previoupage;
      } elseif ( $role == ‘author’ ) {
      //Redirect authors to the previous page
      $redirect = $previoupage;
      } elseif ( $role == ‘customer’ || $role == ‘subscriber’ ) {
      //Redirect customers and subscribers to the previous page
      $redirect = $previoupage;
      } else {
      //Redirect any other role to the previous visited page
      $redirect = $previoupage;
      }
      return $redirect;
      }
      add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );
      ?>

  5. Hi Nicola! Happy new year!
    Sorry to bother you, but… I cannot get this working. 🙁
    What if I put the following in my functions.php and why doesn’t it work? WordPress keeps on making me land on /profile.php page… 🙁

    function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
    $role = $user->roles;
    $myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
    //$myaccount = site_url(8216;/mio-account/’, ‘https’);
    if ( ! admin && $role == ‘customer’ ) {
    //Redirect customers and subscribers to the “My Account” page
    $redirect = $myaccount;
    }
    return $redirect;
    }
    add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );

    1. Hey Alessandro,
      This does not really make sense:

      if ( ! admin && $role == ‘customer’ ) {
      

      You should get a Fatal error on that because of ! admin.

  6. Hello,

    Thanks for your code snippet. It seems to be working well, however, I’d like to somehow redirect users back to the checkout page once logged in/registered successfully. I tried your code and seems to be partially working, and I also tried Gabrielle’s suggested version above, but none of them redirects the user back to the previous/referring page (checkout). They are being redirected to /my-account/.

    I’ve been looking for a solution for days, and have tried every single possible snippet I’ve come across, but with no success so far. How can I redirect users to the checkout page (which in this case would be the referrer) after login/registration form submission on My Account page?

    So the scenario would be: No logged in user -> checkout page -> login page -> logged in -> checkout page.

    Thanks a bunch!

  7. This is very helpful, but I can’t seem to get it to work. I’ve got a role called judge, but logging in as the judge does not redirect.

    Here’s the code I’m using:
    function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
    $role = $user->roles[0];
    $dashboard = admin_url();
    $myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
    if( $role == ‘administrator’ ) {
    //Redirect administrators to the dashboard
    $redirect = $dashboard;
    } elseif ( $role == ‘judge’ ) {
    //Redirect judges to panel
    $redirect = ‘http://www.somesite.com/wp-admin/admin.php?page=entries-index;’;
    } else {
    //Redirect any other role to the previous visited page or, if not available, to the home
    $redirect = wp_get_referer() ? wp_get_referer() : home_url();
    }
    return $redirect;
    }
    add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );

    1. I just noticed that I had a semi-colon in there at the end of the URL for the judge redirect. But after removing it, it’s still not working.

    2. Hi Andrew,
      Right after this line

      //Redirect judges to panel
      

      Add a new line with this code:

      die( 'here' );
      

      Then try to log in with a Judge account and let me know if you see that here on your screen somewhere. You can change the here to anything you want.

      1. Thanks for the help. I ended up finding another way to do it, but I’m going to keep this code handy in case it’s useful elsewhere.

Leave a Reply

Your email address will not be published. Required fields are marked *