Skip to content

Instantly share code, notes, and snippets.

@mathetos
Created November 5, 2014 04:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathetos/435b0e15d30d7a91c9d3 to your computer and use it in GitHub Desktop.
Save mathetos/435b0e15d30d7a91c9d3 to your computer and use it in GitHub Desktop.
Conditional Custom Login Redirects in WordPress
function wip_login_redirect( $url, $request, $user ){
// Define referrer
$slug = $_SERVER["REQUEST_URI"];
// Define slugs of any referrers you want here
// For example, if they page you want to target
// is www.testsite.com/hello-world
// you'll want this:
// $islogin = strpos($slug, 'hello-world');
$islogin = strpos($slug, 'log-in');
// Need to tap into the WP_User Object
// And make sure they are a user
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
// If user is an Admin and they're
// logging in at our custom login page
// redirect them to wp-admin
if(($user->has_cap('edit_users')) && ($islogin==true)) {
$url = admin_url('index.php');
// But, if they are logging in at
// our custom login page and ARE NOT
// an Admin, redirect them to the homepage
} elseif ((!$user->has_cap('edit_users')) && ($islogin==true)) {
$url = home_url();
// If neither of those is true,
// They the user is logging in via a front-end form
// So it's best to just redirect them to that page
} else {
$url = $slug;
}
}
return $url;
}
add_filter('login_redirect', 'wip_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment