Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

WordPress WooCommerce Login Redirect

WordPress WooCommerce Login Redirect.

I am trying to redirect my login based upon the page a customer logs in on. There is certain content that a customer needs to log in to see.  So if they land on
https://MYSITE.com/protected-content the are forwarded to
https://MYSITE.com/protected-login  so that they must login to see the content
There is a conditional that says if they are on this page (https://MYSITE.com/protected-login), the login redirect should be to
https://MYSITE.com/protected-content, basically takes them back to the original page.  The reason for this is there are a bunch of different protected content areas that a variety of user elements must be met.

The function mogobox_kiwanis_inspect_page_id() works but it does not load my filter.  And if it does load my filter, it does not work.

I should note that the following 2 functions are in a Custom Plugin and not in functions.php. Also, there are no errors being reported.

<?php
function mogobox_kiwanis_login_redirect($redirect) {
         $redirect_page_id = url_to_postid( $redirect );
    $checkout_page_id = wc_get_page_id( 'checkout' );
    
    if( $redirect_page_id == $checkout_page_id ) {
        return $redirect;
    }
 
    return wc_get_page_permalink( 'protected-content' );
}


add_action( 'wp', 'mogobox_kiwanis_inspect_page_id', 1500,2 );
function mogobox_kiwanis_inspect_page_id() {

    $page_id = get_queried_object_id();
    $page_id = "$page_id";
    
    $kiwanis = '78430';
    if ($page_id == $kiwanis) {
        add_filter('woocommerce_login_redirect', 'mogobox_kiwanis_login_redirect', 1601, 2);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Granlund

ASKER

@chris
Yes, it is on page ID 78430.
Yes, I am using the Woo Login Functionality [woocommerce_my_account]
I use this: return wc_get_page_permalink( 'shop' ); it still goes back to the same login page but it is not the account page.

There is on part that I left out (some info has changed for out purposes) There is a redirect that is happening prior to the login.

If you go to a category and you are not logged in you are forwarded. If you are logged in and do not have a user data field, you are forwarded.  Once you sign in, you are directed back to the page that originally forwarded you.   But it never leaves the login page.

function mogobox_redirect_page() {
if( ! current_user_can('editor') || ! current_user_can('administrator') ) { 
    $current_user_id = get_current_user_id();
    $member_institution = mogobox_get_userdata($current_user_id, 'user_institution');
    $cat = single_cat_title("", false);

    if ($cat == 'Mu-Phi-Epsilon') {
        if ($member_institution != 'mu-phi-epsilon') {
            wp_redirect(home_url('/mu-phi-epsilon-login/'));
            exit();
        }
    }
    if ($cat == 'Kiwanis') {
        if ($member_institution != 'kiwanis') {
            wp_redirect(home_url('/kiwanis-login/'));
            exit();
        }
    }
}
}

/**
 * Redirect to shop after login.
 *
 * @param $redirect
 * @param $user
 *
 * @return false|string
 */


function mogobox_kiwanis_login_redirect($redirect) {
         $redirect_page_id = url_to_postid( $redirect );
    $checkout_page_id = wc_get_page_id( 'checkout' );
    
    if( $redirect_page_id == $checkout_page_id ) {
        return $redirect;
    }
 
    return wc_get_page_permalink( 'shop' );
}


add_action( 'wp', 'mogobox_kiwanis_inspect_page_id', 1500,2 );
function mogobox_kiwanis_inspect_page_id() {

    $page_id = get_queried_object_id();
    $page_id = "$page_id";
    
    $kiwanis = '78430';
    if ($page_id == $kiwanis) {
        add_filter('woocommerce_login_redirect', 'mogobox_kiwanis_login_redirect', 1601, 2);
    }
}

Open in new window