Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.