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?
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?
Share Improve this question edited May 20, 2020 at 15:57 ermSO asked May 20, 2020 at 15:52 ermSOermSO 3411 gold badge2 silver badges8 bronze badges1 Answer
Reset to default 1The code logic above may not evaluate to true, refactor it like so:
function custom_level_login_redirect() { $isPageLogin = is_page('login'); $isUserLoggedIn = is_user_logged_in(); $level = (int) $_GET['level']; if( $isPageLogin && isUserLoggedin && $level > 0 ) { $redirect_to = esc_url_raw( site_url("membership-account/membership-checkout/?level=" . $level, null) ); exit( wp_redirect( $redirect_to ) ); } return; } add_action('login_init', 'custom_level_login_redirect');
Notice the esc_url_raw(), the $level variable casting, and the if() statement.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742369523a4430981.html
评论列表(0条)