Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Created October 15, 2018 07:21
Show Gist options
  • Save pbrocks/4b3067b468d9c9a2bdbc48e81a6059d2 to your computer and use it in GitHub Desktop.
Save pbrocks/4b3067b468d9c9a2bdbc48e81a6059d2 to your computer and use it in GitHub Desktop.
Redirect users on login
<?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