I’d like to force a user to a specific page upon login based on their role using
if ( current_user_can('contributor') )
and the main login function
function wp_loginout($redirect = '', $echo = true) {
if ( ! is_user_logged_in() )
$link = '<a href="' . esc_url( wp_login_url(get_permalink()) ) . '" rel="nofollow noreferrer noopener">' . __('Log in') . '</a>';
else
$link = '<a href="' . esc_url( wp_logout_url(get_permalink()) ) . '" rel="nofollow noreferrer noopener">' . __('Log out of account') . '</a>';
if ( $echo )
echo apply_filters('loginout', $link);
else
return apply_filters('loginout', $link);
}
I’ve tried a number of combinations and seem to be failing. Any help would be appreciated.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
filter login_redirect:
function my_login_redirect_contributors() {
if ( current_user_can('contributor') ){
return 'url-to-redirect-to';
}
}
add_filter('login_redirect', 'my_login_redirect_contributors');
Method 2
Although this question is a year old hopefully this can help some people.
I ran into a case where the accepted answer didn’t work because the global $current_user was a WP_User object but had null values. The following code is what I found to work. Note the use of the 3rd parameter $user:
function 22352_login_redirect( $redirect_url, $POST_redirect_url, $user ) {
if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'manage_options' ) ) {
$redirect_url = 'url-to-redirect-to';
}
return $redirect_url;
}
add_filter( 'login_redirect', array( $this, 'wpse22352_login_redirect' ), 10, 3 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0