简体   繁体   中英

Bypass WooCommerce My-Account login for Custom login

I'm creating a wholesale members only woocommerce site. I have so far managed to lock all of woocommerce down by creating a redirect back to the my-account page for non logged in users using this bit of code I found here on stack exchange-

add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {

    if ( !is_user_logged_in() && ( is_woocommerce() || is_shop() || 
is_cart() || is_checkout() ) ) {
        wp_redirect( get_permalink( 
get_option('woocommerce_myaccount_page_id')) );
        exit();
    }
}

It works perfectly, but I want to use a custom login page that I created. www.example.com/customlogin - Where/How would I insert this into the above code? I tried but got a syntax error and spent half the day locked out of my site lol.

Next I need to tie the custom login page to the woocommerce my-account page so that I can bypass the default login that woocommerce hides in code on the same page. So basically if a "non-logged-in user" clicks on the "my account" link they are taken to the custom login page instead. This way I'll be able to edit the login page and my-account pages separately which I cant really do when theyre on the same page and all of my editing effect both simultaneously.

Thanks.

You should try the code below that will solve both of your requirements:

add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {

    if ( ! is_user_logged_in() && ( is_woocommerce() || is_shop() ||
is_cart() || is_checkout() || is_account_page() ) ) {
        wp_redirect( home_url( '/customlogin' ) );
        exit();
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

To extend condition to my account pages, I use the conditional WC tag: is_account_page()
For the redirection to your custom form I use: wp_redirect( home_url( '/customlogin' ) );

This code is tested and works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM