admin管理员组文章数量:1334312
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.
本文标签: Redirect Logged In User if page is wploginphp and Get39level39X
版权声明:本文标题:Redirect Logged In User if page is wp-login.php and $_Get['level'] = X 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742369523a2461955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论