Created
March 17, 2018 17:23
-
-
Save craigsimps/e07ef2da74f5e6089f5e00b5763fd7a0 to your computer and use it in GitHub Desktop.
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 | |
add_filter( 'rcp_login_redirect_url', __NAMESPACE__ . '\\login_redirect', PHP_INT_MAX, 2 ); | |
/** | |
* @param string $redirect URL to redirect user to. | |
* @param object $user WP_User object | |
* | |
* @return mixed | |
*/ | |
function login_redirect( $redirect, $user ) { | |
$logged_in_user = get_userdata( $user->ID ); | |
if ( ! isset( $logged_in_user->roles ) || ! is_array( $logged_in_user->roles ) ) { | |
wp_die( 'You have no role assigned.' ); | |
} | |
$redirects = [ | |
/** | |
* If there is no explicit 'redirect_to' given, each role | |
* will fallback to the following ordered redirects upon | |
* logging in. | |
* | |
* role => redirect | |
*/ | |
'administrator' => admin_url(), | |
'subscriber' => home_url( 'members-area' ), | |
]; | |
// Fallback to user role default redirect. | |
foreach ( $redirects as $role => $redirect ) { | |
if ( in_array( $role, $logged_in_user->roles ) ) { | |
return $redirect; | |
} | |
} | |
// No fallback found for role, so proceed as normal. | |
return $redirect; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment