Skip to content

Instantly share code, notes, and snippets.

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 gangesh/5c5d0e943f63262d5d832ef9766e7cec to your computer and use it in GitHub Desktop.
Save gangesh/5c5d0e943f63262d5d832ef9766e7cec to your computer and use it in GitHub Desktop.
WP User Manager - Redirect users to different pages after login based on use role
<?php
function wpum_set_logged_in_user( $user_login, $user ) {
if ( ! defined( 'WPUM_VERSION' ) || version_compare( WPUM_VERSION, '2.1.12', '>' ) ) {
return;
}
wp_set_current_user( $user->ID );
}
add_action( 'wp_login', 'wpum_set_logged_in_user', 10, 2 );
function wpum_conditional_role_redirect_after_login( $location ) {
if ( ! isset( $_POST['wpum_form'] ) || 'login' !== $_POST['wpum_form'] ) {
return $location;
}
$user = wp_get_current_user();
if ( ! $user ) {
return $location;
}
if ( in_array( 'author', (array) $user->roles ) ) {
$location = home_url( '/author-page' );
}
if ( in_array( 'subscriber', (array) $user->roles ) ) {
$location = home_url( '/subscriber-page' );
}
return $location;
}
add_filter( 'wp_redirect', 'wpum_conditional_role_redirect_after_login' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment