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.