Redirect users on login based on user role

The Membership 2 Pro redirects all users upon login to an account page. There's a Redirect Control add-on but it works for all, no option to set by "user role".

The Peter's Login Redirect doesn't work. How can we redirect only authors, admins and contributors to the /wp-admin instead of account page?

  • Ash

    Hello eTech

    Please try the following code:

    function my_login_redirect( $redirect_to, $request, $user, $obj ){
    	if( ! is_user_logged_in() ) return $redirect_to;
    	$user = wp_get_current_user();
    	$allowed_roles = array( 'administrator', 'author', 'contributor' );
        if( array_intersect( $allowed_roles, $user->roles ) ) {
            $redirect_to = admin_url();
        } else {
            $redirect_to = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT );
    	return $redirect_to;
    add_filter('ms_controller_frontend_login_redirect', 'my_login_redirect', 9999, 4 );

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.