Forked from wp-user-manager/wpum_conditional_role_redirect_after_login.php
Created
November 6, 2019 14:10
-
-
Save gmatta01/5c5d0e943f63262d5d832ef9766e7cec to your computer and use it in GitHub Desktop.
WP User Manager - Redirect users to different pages after login based on use role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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