Skip to content

Instantly share code, notes, and snippets.

@IacopoC
Created September 30, 2019 11:35
Show Gist options
  • Save IacopoC/9d5caa8b249a1a647c176ddaea8ba0ff to your computer and use it in GitHub Desktop.
Save IacopoC/9d5caa8b249a1a647c176ddaea8ba0ff to your computer and use it in GitHub Desktop.
Redirect to a custom page when user is logged in based on user role in WP
function hide_the_dashboard()
{
global $current_user;
// is there a user ?
if ( is_array( $current_user->roles ) ) {
// substitute your role(s):
if ( in_array( 'author', $current_user->roles ) ) {
// hide the dashboard:
remove_menu_page( 'index.php' );
}
}
}
add_action( 'admin_menu', 'hide_the_dashboard' );
function the_login_redirect( $redirect_to, $request, $user )
{
// is there a user ?
if ( is_array( $user->roles ) ) {
// substitute your role(s):
if ( in_array( 'author', $user->roles ) ) {
// pick where to redirect to, in the example: Posts page
return admin_url( 'edit.php' );
} else {
return admin_url();
}
}
}
add_filter( 'login_redirect', 'the_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment