Woocommerce/WordPress – Redirect User Login to Homepage

I have searched for the answer to this, used plugins and still nothing works.
I would like users of my site to be redirected to the home page after they login/register.

Currently, the user logs in and is redirected to the my account page.

Read More

Woocommerce provides this code, but it failed to work for me:

/*
 * goes in theme functions.php or a custom plugin
 *
 * By default login goes to my account
 **/
add_filter('woocommerce_login_widget_redirect', 'custom_login_redirect');

function custom_login_redirect( $redirect_to ) {
     $redirect_to = 'http://anypage.com';
}

I have also attempted to use the Peter’s Redirect plugin but it does not work since woocommerce bypasses wp-login.php.

Thoughts?

Related posts

Leave a Reply

8 comments

  1. You can download http://wordpress.org/extend/plugins/peters-login-redirect/ and replace this in the widget-login.php

    <input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo $redirect_to; ?>
    

    with

    <input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo site_url('/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php/'); ?>" />
    

    That way you can use peters login redirect to redirect globally and specific users using the woocommerce login widget.

    Make sure you change “Use external redirect file. Set this to “Yes” if you are using a plugin such as Gigya that bypasses the regular WordPress redirect process (and allows only one fixed redirect URL). Then, set the redirect URL to %s” in the settings to “YES”.

    Hope this helps.

  2. Use this code on functions.php

    add_filter('woocommerce_login_redirect', 'wc_login_redirect');
    function wc_login_redirect( $redirect_to ) {
         $redirect_to = 'https://www.google.co.in/';
         return $redirect_to;
    }
    
  3. That fix they provide only works if you are utilizing the login widget. All you need to do it redirect the user after logging in to your home page is to add this to your login form:

    <input type="hidden" name="redirect" value="<?php echo get_home_url(); ?>" />
    
  4. Do this:

    Go to admin > Woocommerce > Settings > General

    Find “Customer Accounts” under “Cart, Checkout and Accounts”

    Uncheck “Prevent customers from accessing WordPress admin”

    Save Changes and test!

  5. You can set redirect according to user role. Given bellow the code for that redirection and I have developed a WooCommerce Login or Register Redirect plugin for entry level user or nor technical or no-programmer.

     //Redirect users to custom URL based on their role after login
    
    function wp_woo_custom_redirect( $redirect, $user ) {
    
    // Get the first of all the roles assigned to the user
    $role = $user->roles[0];
    $dashboard = admin_url();
    $myaccount = get_permalink( wc_get_page_id( 'my-account' ) );
    
    if( $role == 'administrator' ) {
    
        //Redirect administrators to the dashboard
        $admin_redirect = get_option('admin_redirect');
        $redirect = $admin_redirect;
    } elseif ( $role == 'shop-manager' ) {
    
        //Redirect shop managers to the dashboard
        $shop_manager_redirect = get_option('shop_manager_redirect');
        $redirect = $shop_manager_redirect;
    } elseif ( $role == 'customer' || $role == 'subscriber' ) {
    
        //Redirect customers and subscribers to the "My Account" page
        $customer_redirect = get_option('customer_redirect');
        $redirect = $customer_redirect;
    } else {
    
        //Redirect any other role to the previous visited page or, if not available, to the home
        $redirect = wp_get_referer() ? wp_get_referer() : home_url();
    }
    return $redirect;
    }
    add_filter( 'woocommerce_login_redirect', 'wp_woo_custom_redirect', 10, 2 );
    
  6. Also include this for standard WordPress login form logins that use wp-login.php:

    add_filter('login_redirect', 'custom_login_redirect');
    

    I use this filter and the one you have given, against the same filter function, and it catches the two places where a user may log in (WP and WC).

    Also just noticed the obvious problem. You have this in your filter function:

    $redirect_to = 'http://anypage.com';
    

    but you need this:

    return 'http://anypage.com';
    

    $return_url is passed in from previous filters, so you can inspect it and decide whether to change it.

    Edit:

    Actually, I need to correct some of this. It looks like the woocommerce_login_widget_redirect filter is used to set the redirect parameter in the widget login form. This means it only fires when the user is not logged in and the widget login form is presented. It cannot be used to decide where to send the user after they have logged in.

    The WP login_redirect filter fires after the user has logged in, and can modify any redirect_to URL sent with the login form to enable you to redirect the user to some other page.

    In addition, the WooCommerce login widget does not handle the logins that you would assume it does. The login process is handled via AJAX in woocommerce-ajax.php You will see in there that the redirect URL is not passed through the woocommerce_login_widget_redirect filter and so cannot be modified. I’m going to raised this as a pull request with WooCommerce, because I believe it should be filtered.

    https://github.com/woothemes/woocommerce/pull/2508