1

I'm trying to build a code where non-members get redirected to the landing page. Unfortunately, the code that I've built works partly and results in a redirect loop.

if(is_user_logged_in() && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()) {
    global $current_user;
    $current_user->membership_level = pmpro_getMembershipLevelForUser($current_user->ID);
    echo 'Membership Level: ' . $current_user->membership_level->name;
} else {
    wp_redirect(/my-account/orders/);
    exit;
}

Any help would be appreciated. The first part of the if statement is working fine but the second half is returning redirect loop.

Thanks and Regards,

Piyush

1

You also need to check if the user is on that particular page or not. If he is on that page, then there is no need to redirect him/her again.

Change your else to this:

if(is_user_logged_in() && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()) {
    global $current_user;
    $current_user->membership_level = pmpro_getMembershipLevelForUser($current_user->ID);
    echo 'Membership Level: ' . $current_user->membership_level->name;
} else {
    if( ! is_page('page-slug') ){
        wp_redirect('/my-account/orders/');
        exit;
    }
}

In which page-slug is the slug of the page you are redirecting your users to.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.