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/:


<?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' ) );
if( $role == 'administrator' ) {
//Redirect administrators to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'shop-manager' ) {
//Redirect shop managers to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'editor' ) {
//Redirect editors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'author' ) {
//Redirect authors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$redirect = $myaccount;
} 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 );

view raw

functions.php

hosted with ❤ by GitHub

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.

33 replies
  1. rbaccaro
    rbaccaro says:

    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,

    Reply
  2. jamie
    jamie says:

    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

    Reply
  3. siva
    siva says:

    Hello sir,

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

    Reply
  4. Gabriele
    Gabriele says:

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

    Reply
    • Gabriele
      Gabriele says:

      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 );
      ?>

      Reply
  5. Alessandro Marengo
    Alessandro Marengo says:

    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( ‘/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 );

    Reply
  6. Alex P.
    Alex P. says:

    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!

    Reply
  7. Andrew
    Andrew says:

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

    Reply
    • Andrew
      Andrew says:

      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.

      Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
  8. rameez
    rameez says:

    Hi
    I’m new to wordpress
    Can u plz tell me what changes do I need to make in the script If I want to redirect my site members based on member type I made using “BuddyPress Member Type Generator by Buddydev”?

    Reply
  9. Power Genius
    Power Genius says:

    Thanks so much for this code. Was extremely helpful. We have custom role types for some of our users and I couldn’t get this to work with any redirection plugins, but this has worked a treat.

    Reply
  10. Dana
    Dana says:

    I am actually trying to give subscribers access to the dashboard where they can see the points earned in their course but nothing I have tried seems to work. I copied your code above and changed the line near the end about redirecting customers and subscribers to manage account and replaced it with “to the dashboard.” I was happy to have not actually broken my blog doing this, but nothing changed. Any ideas?

    Reply
  11. davelow
    davelow says:

    thanks for the codes!
    I realised the codes work only if logging in from WC my-account page.
    what can i do to make the codes work even when logging in from the default login.php?

    Reply
    • davelow
      davelow says:

      I used this instead..

      /**
      * Redirect subscriber after successful login.
      *
      * @param string $redirect_to URL to redirect to.
      * @param string $request URL the user is coming from.
      * @param object $user Logged user’s data.
      * @return string
      */

      function subscriber_login_redirect( $redirect_to, $request, $user ) {
      //is there a user to check?
      if (isset($user->roles) && is_array($user->roles)) {
      //check for subscribers
      if (in_array(‘subscriber’, $user->roles)) {
      // redirect them to another URL, in this case, the homepage
      $redirect_to = home_url();
      }
      }

      return $redirect_to;

      }

      add_filter( ‘login_redirect’, ‘subscriber_login_redirect’, 10, 3 );

      Reply

Trackbacks & Pingbacks

  1. […] I setup a modified Woocommerce login form, to work with the membership plugin that I am using (Paid Membership Pro) with the help of this blog: Redirect after logging in. […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply