Redirect wordpress user to another page based on assigned role

Does anyone know a way (or plugin) that would allow me to automatically redirect users to another page, based on their assigned user role, if they try accessing the main /wp-admin page?

The tricky part is (I guess), is that I still need them to be able to access sub-admin pages (ie. mysite.com/wp-admin/edit.php) — it would only redirect them if they tried going to the main/dashboard page, mysite.com/wp-admin

Related posts

2 comments

  1. I’ve recently had the problem where I needed to redirect multiple pages. The following code will check the roles you want to redirect ($valid-roles) and if not valid redirect to a given URL… in this case its /access-denied/.

    Note: The page ID’s in the array list of pages to re-direct.

    add_action( 'template_redirect', 'role_based_redirect' );
    function role_based_redirect() {
        if( is_page( array( 1488, 2413, 2379, 2265, 2396, 2370, 2366, 4600 ) ) ) { //check the list of "corporate" pages
            $user = wp_get_current_user();
            $valid_roles = [ 'administrator', 'corporate', 'editor' ];
    
            $the_roles = array_intersect( $valid_roles, $user->roles );
    
            // The current user does not have any of the 'valid' roles.
            if ( empty( $the_roles ) ) {
                wp_redirect( home_url( '/access-denied/' ) );
                exit;
            }
        }
    }
    
    
  2. There are many ways you can set the URL redirect to a different link by role on your WordPress page, you may use a plugin or you can also build a plugin to customize it specifically on your own way follow this link for that instruction but I believe as a second and better option this plugin right here might be easy for you might help you as well.

    Good luck

Comments are closed.