I have read various post about redirecting a logged in user if the go to /wp-login.php. However, my case is a little different. I want my users to click a link that takes them to "/wp-login.php?action=register&level=7", for example. If the User is already logged in, then they should be redirected to "membership-account/membership-checkout/?level=7". I have tried the following code.
function custom_level_login_redirect()
{
$isPageLogin = is_page('login');
$isUserLoggedIn = is_user_logged_in();
$level = $_GET['level'];
if( is_page('login') && is_user_logged_in() && !empty($_GET['level']) && is_numeric($_GET['level'])) {
$level = intval($_GET['level']);
$redirect_to =
site_url("membership-account/membership-checkout/?level=" . (string) $level, null);
wp_redirect( $redirect_to );
exit();
}
return;
}
add_action('login_init', 'custom_level_login_redirect');
The above code does not execute on "login-init". When I tried "wp" as the action then "level" url parameter was not available. The code does not execute if I set it up as filter and use "login_redirect" as the hook. I always get redirected to "?1589985914" which just means the index (home) page.
What action or filter should I hook into to redirect an already logged in user who is sent to the wp-login.php page with url parameters?