0

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.

   function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
global $current_user;

    $level = pmpro_getMembershipLevelForUser($user->ID);
    $myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );

if          ( $level->id == 2 ) {
            $redirect = '/rookie-dashboard/';
} elseif    ( $level->id == 3 ) {
            $redirect = '/player-dashboard/';
} elseif    ( $level->id == 4 ) {
            $redirect = '/all-star-dashboard/';
} elseif    ( $level->id == 5 ) {
            $redirect = '/coach-dashboard/';
} elseif    ( $level->id == 6 ) {
            $redirect = '/owner-dashboard/';
} elseif    ( $level->id == 7 ) {
            $redirect = 'https://corporate.example.com/dashboard/';
} elseif    ( $level->id == 8 ) {
            $redirect = '/dashboard/';

} else {
    $redirect = $myaccount;
}
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

However, I was wondering if it's possible to add a referrer to this function. Basically I want the function to first check to see if a specific page was requested, if so then they will be redirected to /member-login/ because the site is locked down, once logged in it would go to the specific page they originally requested. If the user didn't request a specific page then they would be redirected to the appropriate dashboard like I had above.

This was in the original code:

$redirect = wp_get_referer() ? wp_get_referer() : home_url();

However, I'm not sure how to apply it.

Thanks!

0

So the solution was actually pretty simple.

First I needed to modify my custom member login redirect to include the redirect_to portion in the redirect URL:

function pb4r_redirect()
{
    global $current_user;
    $refer=urlencode($_SERVER["REQUEST_URI"]);
    $okay_pages = array(4, 10, 12, 13, 191, 200, 1380, 1381, 1382, 1383, 1449, 1631, 1643, 1652, 1653, 1654, 1666, 1718, 1725, 1840, 'Checkout', 'Cart', pmpro_getOption('billing_page_id'), pmpro_getOption('account_page_id'), pmpro_getOption('levels_page_id'), pmpro_getOption('checkout_page_id'), pmpro_getOption('confirmation_page_id'));

   //if the user is logged out

 if(!$current_user->ID
     && !is_home()
     && !is_page($okay_pages)
     && !strpos($_SERVER['REQUEST_URI'], "login")
     && !is_singular( 'post' )
     && !is_product()
    )
    {
        wp_redirect( '/member-login' . '?redirect_to=' . $refer );
        exit;
    } 
}
add_action('template_redirect', 'pb4r_redirect');

Then I modified the woocommerce function to include the redirect_to:

function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
global $current_user;

    $level = pmpro_getMembershipLevelForUser($user->ID);
    $myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );

if( isset( $_REQUEST['redirect_to'] ) ) {
        return $_REQUEST['redirect_to'];
}elseif      ( $level->id == 2 ) {
            $redirect = '/rookie-dashboard/';
} elseif    ( $level->id == 3 ) {
            $redirect = '/player-dashboard/';
} elseif    ( $level->id == 4 ) {
            $redirect = '/all-star-dashboard/';
} elseif    ( $level->id == 5 ) {
            $redirect = '/coach-dashboard/';
} elseif    ( $level->id == 6 ) {
            $redirect = '/owner-dashboard/';
} elseif    ( $level->id == 7 ) {
            $redirect = 'https://corporate.example.com/dashboard/';
} elseif    ( $level->id == 8 ) {
            $redirect = '/dashboard/';

} else {
    $redirect = $myaccount;
}
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

Now I am able to redirect users to the requested page. If they didn't request a page then they are automatically redirected to their dashboard. If they are a registered user but aren't part of a membership program then they are redirected to the My Account page.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.