Skip to content

Instantly share code, notes, and snippets.

@BoyetDgte
Created February 26, 2020 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoyetDgte/d0627b1e81e2929e95314d09be9425b5 to your computer and use it in GitHub Desktop.
Save BoyetDgte/d0627b1e81e2929e95314d09be9425b5 to your computer and use it in GitHub Desktop.
Redirect users to a different URL(default is My Account) after WooCommerce login.
// After login, users (customers and subscribers only) are redirected to the "Video Library" page -> get_permalink( $post = 6913 );
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
// video library URL
$videolibrary = get_permalink( $post = 6913 );
if( $role == 'administrator' || $role == 'shop-manager' || $role == 'editor' || $role == 'author' ) {
//Redirect above roles to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "Video Library" page
$redirect = $videolibrary;
} 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', 'wc_custom_user_redirect', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment