Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Created May 30, 2016 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardobrg/9a3b18b4329de069ffb24e756b68ab45 to your computer and use it in GitHub Desktop.
Save ricardobrg/9a3b18b4329de069ffb24e756b68ab45 to your computer and use it in GitHub Desktop.
Simple WordPress login redirect. Can be used as a mu-plugin or in functions.php
<?php
function brg_login_redirect( $redirect_to, $request, $user ) {
global $user;
//if there's a user with defined roles
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check if it's admin
if ( in_array( 'administrator', $user->roles ) ) {
//admins can go anywhere
return $redirect_to;
} else {
//non-admins go to home
return home_url();
}
//there's no user roles to check
} else {
//go home
return home_url();
}
}
add_filter( 'login_redirect', 'brg_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment