Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active September 6, 2017 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgee/3e63c75cfca03389f4627004004da009 to your computer and use it in GitHub Desktop.
Save neilgee/3e63c75cfca03389f4627004004da009 to your computer and use it in GitHub Desktop.
Redirect a WooCommerce Customer to Shop Page on Login
<?php
// Ref - https://nicola.blog/2016/03/30/redirect-after-logging-in-based-on-the-user-role/
// add me to functions.php
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
/**
* Redirect users to custom URL based on their role after login
*
* @param string $redirect
* @param object $user
* @return string
*/
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();
$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
$shop = get_permalink( wc_get_page_id( 'shop' ) );
$wp_role = array( 'administrator','shop-manager','editor','author' ); // add roles that go dashboard
if ( in_array($role , $wp_role , TRUE)) {
//Redirect to the dashboard
$redirect = $dashboard;
}
else {
// Redirect to the shop page
$redirect = $shop; // Everyone else goes to Shop page
}
return $redirect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment