When you use WooCommerce login form (usually my-account page), by default users are redirected to my-account page after successful login. Might not be the best way to showcase your shop. Some user might want to redirect logged in users to a custom page, some might consider redirecting to shop page. We can achieve this using woocommerce_login_redirect
filter.
function careless_login_redirect( $redirect, $user ) {
//check logged in user
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect admin users to wp-admin
$redirect = admin_url();
} else {
//non-admin users are redirected to shop page
$redirect = wc_get_page_permalink( 'shop' );
}
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'careless_login_redirect', 999, 3 );
If you want to redirect non-admin users to some custom page, instead of shop page, replace line 10 with $redirect = home_url( 'CUSTOM_PAGE_SLUG' );
– replace CUSTOM_PAGE_SLUG
with your actual page slug.
Code goes in the functions.php
file of your active theme.
Note: woocommerce_login_redirect
runs too early. So global $current_user
or wp_get_current_user()
will not work.