WordPress
198 views

Redirecting WordPress to WooCommerce Login

Redirecting WordPress to WooCommerce Login

In the intricate web of WordPress customization, user experience often takes center stage. One stumbling block for many site owners is the default WordPress login page, a feature that might not seamlessly align with the overall site aesthetics, especially for those relying on WooCommerce.

Unpacking the Code


        /**
         * Redirect default WordPress login to WooCommerce login page for visitors
         */
        function redirect_wp_login_to_woocommerce() {
            // Check if the user is not logged in
            if (!is_user_logged_in()) {
                // Check if the current page is the default WordPress login page
                if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
                    // Set a cookie to store the referring page URL
                    setcookie('referring_page', esc_url($_SERVER['HTTP_REFERER']), time() + 3600, '/');
                    
                    // Redirect to the WooCommerce login page
                    wp_redirect(wc_get_page_permalink('myaccount'));
                    exit;
                }
            }
        }

        // Hook the function to the 'init' action
        add_action('init', 'redirect_wp_login_to_woocommerce');
    

How it Works

  1. User Check: The function first ensures the user is not logged in (!is_user_logged_in()).
  2. Identifying Login Page: It then pinpoints if the current page is the default WordPress login page (wp-login.php).
  3. Cookie Setup: If the conditions are met, a cookie named ‘referring_page’ is set to remember the URL of the page they came from ($_SERVER['HTTP_REFERER']).
  4. WooCommerce Redirection: Users are then seamlessly redirected to the WooCommerce login page using wp_redirect(wc_get_page_permalink('myaccount')).

Integration Guide

  1. Locate functions.php: Find your theme’s functions.php file through your WordPress dashboard or FTP.
  2. Insert the Code: Copy and paste the provided PHP code at the end of functions.php.
  3. Save Changes: Ensure you save the file after inserting the code.
  4. Test It Out: Log out of your WordPress account and visit the default login page. You should now be redirected to the WooCommerce login page.

In Conclusion

With this simple PHP function, you’ve streamlined the login experience on your WordPress site. Visitors will now smoothly navigate to the WooCommerce login page, aligning the user journey with your site’s overall design. A modest tweak with a noteworthy impact, showcasing the flexibility of WordPress customization. Happy redirecting!

Tags: Redirecting WordPress, WooCommerce, , WordPress
You must be logged in to post a comment.
Exit mobile version