skip to Main Content

We’ve a WoordPress with WooCommerce (which we are not using at the moment, but don’t want to get rid of), and when users sign up and follow the set up password instructions, they end up in oursite.com/wp-login.php.

The problem comes when they log in, because they’re automatically redirected to WooComerce’s My Account page. We would like to take users to a different page.

Is it possible to disable this WooCommerce functionality? Choose a different page or even go to the homepage instead?

Thanks!

4

Answers


  1. Chosen as BEST ANSWER

    Solved, although I still don't understand the "architecture" of it...

    Why do I have to force the redirection using an extra hook in my functions file, instead of disabling WooCommerce's behaviour?

    Anyway :)

    /**
     * Redirect user after successful login.
     *
     * @param string $redirect_to URL to redirect to.
     * @param string $request URL the user is coming from.
     * @param object $user Logged user's data.
     * @return string
     */
    function my_login_redirect( $redirect_to, $request, $user ) {
        //is there a user to check?
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
                //check for admins
                if ( in_array( 'administrator', $user->roles ) ) {
                        // redirect them to the default place
                        return $redirect_to;
                } else {
                        //return home_url();
                        $redirect = get_page_link( 25159 );
                        return $redirect;
                }
        } else {
                return $redirect_to;
        }
    }
    
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
    

  2. I would use this to redirect a user to the home page:

    add_filter( 'login_redirect', 'custom_login', 10, 3 );
    
    function custom_login( ){
        global $wp_query;
        if (is_account_page()) {
            wp_safe_redirect(home_url());
            exit();
        };
    };
    
    
    Login or Signup to reply.
  3. WooCommerce has two hooks for redirect after registration and login:

    <?php
    /**
     * Redirect to shop after login.
     */
    add_filter( 'woocommerce_login_redirect', 'ywp_login_redirect', 1100, 2 );
    function ywp_login_redirect( $redirect, $user ) {
        if ( isset( $_GET['redirect_to'] ) ) {
            $redirect = esc_url( $_GET['redirect_to'] );
        } else {
            $redirect = home_url();
        }
    
        return $redirect;
    }
    
    /**
     * Redirect after registration.
     */
    add_filter( 'woocommerce_registration_redirect', 'ywp_register_redirect' );
    function ywp_register_redirect( $redirect ) {
        if ( isset( $_GET['redirect_to'] ) ) {
            $redirect = esc_url( $_GET['redirect_to'] );
        } else {
            $redirect = home_url();
        }
    
        return $redirect;
    }
    

    UPDATE

    This code is also used to change the wp-admin/wp-login.php redirect:

    add_filter( 'login_redirect', 'ywp_redirect_wp_login' );
    function ywp_redirect_wp_login() {
            if ( isset( $_GET['redirect_to'] ) ) {
                $redirect = esc_url( $_GET['redirect_to'] );
            } else {
                $redirect = home_url();
            }
        
            return $redirect;
    }
    

    Code goes in functions.php file of your active theme/child theme. Tesed and works.

    Login or Signup to reply.
  4. WooCommerce uses this filter to force redirect customers to my-account page woocommerce_prevent_admin_access, so you need to pass false to this filter to disable the woocommerce redirect

    add_action(
        'admin_init',
        function() {
            add_filter( 'woocommerce_prevent_admin_access', '__return_false', 100 );
        },
        1
    );
    

    You can then use login_redirect filter after that to customize the wordpress redirect link after login

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search