Woocommerce/Wordpress - Redirect User Login to Homepage

Viewed 21045

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.

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?

8 Answers

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 );
Related