Skip to content

Instantly share code, notes, and snippets.

@webmasterninjay
Created February 21, 2015 07:55
Show Gist options
  • Save webmasterninjay/bab0a9a802e0592c2c5c to your computer and use it in GitHub Desktop.
Save webmasterninjay/bab0a9a802e0592c2c5c to your computer and use it in GitHub Desktop.
Redirect user after successful login
<?php
/**
* @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 ja_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'ja_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment