Log in redirect based on user role?

Home Forums User Registration Log in redirect based on user role?

This topic contains 2 replies, has 2 voices, and was last updated by  LFFprop 9 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7169

    LFFprop
    Participant

    How can I redirect a user after logging in, based on his role?

    #7170

    sanjeev3
    Member

    Hello LFFprop,

    Currently, we don’t have such feature. If you could add some codes, here it is:

    add_filter( 'user_registration_login_redirect', 'ur_login_redirect', 10, 2 );
    function ur_login_redirect( $redirect, $user ) {
    
    	$user_meta  = get_userdata( $user_id );
    	$user_roles = $user_meta->roles;
    
    	if ( in_array( 'subscriber', $user_roles ) ) {
    		$redirect = 'sample-page';
    	} elseif( in_array( 'administrator', $user_roles ) ) {
    		$redirect = 'my-account';
    	}
    
    	return $redirect;
    }

    Add the codes to your child theme’s functions.php. If you are unfamiliar with such stuffs, donot add anything, any error might result your site to crash. This is more of a developers guide.

    Regards,
    WPEverest Support

    #7259

    LFFprop
    Participant

    I understand what your are advising, but it doesn’t have any effect.

    I have 2 user roles: ‘author’ and ‘PayingVisitor’. so this is what I added to the functions.php for the child theme:

    /** Log-in redirect based on user role */
    add_filter( ‘user_registration_login_redirect’, ‘ur_login_redirect’, 10, 2 );
    function ur_login_redirect( $redirect, $user ) {

    $user_meta = get_userdata( $user_id );
    $user_roles = $user_meta->roles;

    if ( in_array( ‘author’, $user_roles ) ) {
    $redirect = ‘profiel-bewerken’;
    } elseif( in_array( ‘PayingVisitor’, $user_roles ) ) {
    $redirect = ‘zoeken’;
    }

    return $redirect;
    }

    But still, the ‘My Account’ page appears after logging in.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.