1
votes

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

2
A Google search for manage user roles will turn up many such plugins stackoverflow.com/questions/34812089/… for example. Also codecheese.com/2013/11/10-essential-wordpress-user-role-plugins - Steve
Thanks @Steve I'm not really looking for a role manager. I already have custom user roles defined and both the Members plugin and IE8 Box Hide or whatever installed that allows what they can and can't see on certain admin pages. However, none of those have controls that regulate which pages can be accessed. That's what I'm needing. - edit7279
Ahh I see - sorry thought they looked promising. Last shot might be premium.wpmudev.org/blog/… It says "Only admins can access wp-admin. Everyone else will be re-directed to the homepage." Good luck. - Steve

2 Answers

5
votes

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;
        }
    }
}

0
votes

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