Created
October 15, 2018 07:21
-
-
Save pbrocks/4b3067b468d9c9a2bdbc48e81a6059d2 to your computer and use it in GitHub Desktop.
Redirect users 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 | |
/** | |
* Redirect user to referrer after successful login. | |
* | |
* @param string $redirect_to URL to redirect to. | |
* @param string $request URL the user is coming from. | |
* @param object $user Logged user's data. | |
* @return string | |
*/ | |
function a_login_redirect_to_referrer( $redirect_to, $request, $user ) { | |
// is there a user to check? | |
if ( isset( $user->roles ) && is_array( $user->roles ) ) { | |
if ( in_array( 'administrator', $user->roles ) ) { | |
// redirect admins to dashboard reports page | |
$redirect_to = admin_url( 'admin.php?page=pmpro-reports' ); | |
// redirect to referrer | |
} elseif ( wp_get_referer() ) { | |
$redirect_to = wp_get_referer(); | |
} else { | |
// redirect them to account page | |
$redirect_to = home_url(); | |
} | |
} | |
return $redirect_to; | |
} | |
add_filter( 'login_redirect', 'a_login_redirect_to_referrer', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment