Last active
September 6, 2017 20:56
-
-
Save neilgee/3e63c75cfca03389f4627004004da009 to your computer and use it in GitHub Desktop.
Redirect a WooCommerce Customer to Shop Page on Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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