Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woo Commerce Login Redirect Based off of User Roles

Im looking for a way to redirect my wholesale users.

The way I have my woo-commerce setup for wholesale is to assign users to wholesale based off of the wordpress users role that i named "wholesale".

I would like to retain the redirect of the regular users without change but add a way to send that wholesale role to another costume page.

Iv used Peters Login Redirect and WordPress Login Redirect neither plugin worked even though it has the functionality - The result after the proper settings are entered for the custom page is the default woo commerce my account page.

Is there a way of doing that via functions.php ?

like image 672
MDL Avatar asked Nov 08 '22 22:11

MDL


1 Answers

You can use Peter's Login Redirect or this example directly from the Codex. Simply switch administrator to wholesale or whatever your role is called.

/**
 * Redirect user 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 my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Modified codex example to send users with wholesale role to custom page:

/**
 * Redirect wholesalers to custom page user 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 my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'wholesale', $user->roles ) ) {
            $redirect_to = 'http://example.com/wholesale';
        } 
    } 
    return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
like image 117
helgatheviking Avatar answered Nov 15 '22 13:11

helgatheviking