Send Each Role to Its Own Page After Login
Chooses where people land after logging in based on their role, for example editors to the posts list, customers to their account and subscribers to a members page.
Opens WPIDE on
Code
// Role => destination. {admin} means the wp-admin address.
$wpide_login_destinations = array(
'administrator' => '{admin}',
'editor' => '{admin}edit.php',
'author' => '{admin}edit.php',
'subscriber' => '/',
);
add_filter( 'login_redirect', function ( $redirect_to, $requested, $user ) use ( $wpide_login_destinations ) {
if ( ! $user instanceof WP_User ) {
return $redirect_to;
}
if ( $requested && untrailingslashit( $requested ) !== untrailingslashit( admin_url() ) ) {
return $redirect_to;
}
foreach ( (array) $user->roles as $role ) {
if ( empty( $wpide_login_destinations[ $role ] ) ) {
continue;
}
$target = $wpide_login_destinations[ $role ];
return 0 === strpos( $target, '{admin}' ) ? admin_url( substr( $target, 7 ) ) : home_url( $target );
}
return $redirect_to;
}, 20, 3 );
Notes
Use {admin} at the start of a value for an admin page, or a path on your site. Explicit redirect_to links, such as from a protected page, are respected.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.