Search code examples
phpwordpresswoocommerceuser-accountshook-woocommerce

WooCommerce login area redirect to default Wordpress login form


Woocommerce's "My Account" page shows a register/login form if the user isn't logged in, but I'm trying to use the default Wordpress login/register page (wp-login.php) instead.

How can I make "/my-account" redirect to "wp-login.php" if the user isn't logged in?

Thank you.


Solution

  • Normally the Woocommerce "customer" user role, can't access to the admin of wordpress. But you can use a function hocked in woocommerce_before_customer_login_form hook with a mixed condition to redirect user to classic wordpress login area, this way:

    add_action( 'woocommerce_before_customer_login_form', 'redirect_customer_login_access');
    function redirect_customer_login_access() {
    
        // Here the conditions (woocommerce my account pages and unlogged user)
        if( is_account_page() && !is_user_logged_in()){
    
            // Define here the redirection after login (optional)
            $redirection_after = site_url( '/shop/' );
    
            // Redirecting to Wordpress login area
            wp_redirect( wp_login_url( $redirection_after ) );
    
            // always use exit after wp_redirect() function.
            exit; 
        }
    }
    

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

    This code is tested and works.