1

I want to redirect all users except the admin away from the wp-admin page.
If the logged in user tries to access /wp-admin then they will be redirected back home.
Right now they get a page that says "Sorry you are not allowed to access this page"
Can y'all please help me construct a function in my functions.php file to check for these conditions and redirect?
This is what I have, and it isn't working for what I want.

function acme_login_redirect( $redirect_to, $request, $user  ) {
  return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url();
}
add_filter( 'login_redirect', 'acme_login_redirect', 10, 3 );
1
  • note that if this filter worked it would redirect non-admins on login but there is nothing to prevent users from just visiting WP Admin after they're already logged in
    – Tom J Nowell
    Commented Jan 26, 2022 at 15:57

1 Answer 1

1

Here's how I do this:

// if admin send them to the dashboard, otherwise leave them on the frontend
add_action('wp_login', 'rt_login_redirect', 10, 2);  //use this action if you want them to be redirected only after login but leave the ability to to to the dashboard later (ie changing profile or pw).
add_action( 'admin_init', 'rt_login_redirect' ); // use this action to completely dis-allow all others besides the admin to access the dashboard.
function rt_login_redirect($user_login, $user) {

    if  (!current_user_can('manage_options')) {
            wp_safe_redirect( home_url(), 302);
            exit();
    }
}

The main function checks if the users is an admin by checking if the users has the ability to manage options. If they don't have that capability then the function will redirect them to the homepage. If they do they can stay on the backend.

The function is activated when someone try to take an action (as commented above) (chose an option based on what you want)

5
  • 1
    I adjusted this to use the safe redirect function, and the home_url function. Not all sites live at /.
    – Tom J Nowell
    Commented Jan 26, 2022 at 15:58
  • Thank you so much!! It's perfect!
    – Jed Booth
    Commented Jan 26, 2022 at 22:39
  • Glad to hear. Could you please mark it as answered? (check mark just to the left of the answer).
    – rudtek
    Commented Jan 27, 2022 at 5:39
  • I have one more question. Imagine I have multiple pages for multiple users: wordpresssite.com/orange wordpresssite.com/apple Their username and role is the same as their relative page url. Neither of these pages have clickable links on the home page because I don't want apple to try and get into orange's page. I also have privileges set up so they can't see each other's page. Can I change the home_url() function to look something like this: home_url( $path = current_user, $scheme = 'relative') So that they go to their dedicated page instead of the admin page?
    – Jed Booth
    Commented Jan 27, 2022 at 14:56
  • Yes, that should work.
    – rudtek
    Commented Jan 28, 2022 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.