Redirect Logged In User if page is wp-login.php and $_Get['level'] = X

I have read various post about redirecting a logged in user if the go to wp-login.php. However, my case is a little dif

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

The 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

相关推荐

  • Redirect Logged In User if page is wp-login.php and $_Get['level'] = X

    I have read various post about redirecting a logged in user if the go to wp-login.php. However, my case is a little dif

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信