1

I've set it so non-admin user can't see the "Dashboard" link or can't have access to the main "wp-admin/index.php" page. I've also added a plugin that restrict non-admin users from creating a new page. Whenever a non-admin users try to add a new page, they'll be accidentally redirected to "wp-admin/index.php?permissions_error=true", which is the main "wp-admin/index.php", or "Dashboard" page.

How do I restrict access to the wp-admin/index.php (main dashboard page), or redirect them to their wp-admin/profile.php page?

screenshot of what I'm trying to achieve: If someone were smart enough to add "?permissions_error=true" next the the "wp-admin/index.php" in the url address, the would get access to the "wp-admin/index.php" main dashboard page, even if they don't have permission to.

  • I've tried adding "Redirect 301 /localhost/ft/wp-admin/index.php?permissions_error=true http://google.com" to the .htaccess file, but it's not working because of the "?permissions_error=true"
2

The easiest solution is to hook in load-index.php and redirect non-admin users to their profile. Effectively blocking access to that page. I'm going to wrap the permission check up in a function for this example (we'll use it more than once).

function _wpse206466_can_view()
{
    // or any other admin level capability
    return current_user_can('manage_options');
}


add_action('load-index.php', 'wpse206466_load_index');
function wpse206466_load_index()
{
    if (!_wpse206466_can_view()) {
        $qs = empty($_GET) ? '' : '?'.http_build_query($_GET);
        wp_safe_redirect(admin_url('profile.php').$qs);
        exit;
    }
}

You might also want to remove the admin index page from the admin menu if the users can't access it. To do that, hook into admin_menu, check the capability then remove the page.

add_action('admin_menu', 'wpse206466_remove_index');
function wpse206466_remove_index()
{
    if (!_wpse206466_can_view()) {
        remove_menu_page('index.php');
    }
}

Here is all that as a plugin.

  • Hi, I thought it worked, but then tried to access the wp-admin/index.php today as admin and was redirected to profile.php. It work and redirected non-admin users to their profile page, but it's not allowing admin to the main dashboard page. I've even tested the code and the plugin on a fresh wordpress site. – lee Oct 26 '15 at 8:33
  • There was a small typo in my code. See: github.com/chrisguitarguy/WPSE-Plugins/commit/… The code in this post and the plugin on github has been updated – chrisguitarguy Oct 26 '15 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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