Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Limit Dashboard Access in WordPress

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

If you run a WordPress site with multiple users—authors, editors, or contractors—you may want to keep some of them out of the admin dashboard entirely. By default, any logged-in user with a role that has read capability can access wp-admin. Limiting dashboard access means redirecting those users to the front end of your site (or a custom page) and optionally hiding the admin bar. This guide shows you how to do it safely with a small code snippet or a plugin, and covers the common pitfalls.

What You'll Need

  • A WordPress site with administrator access (you'll be adding code or installing a plugin).
  • Access to your theme's functions.php file via Appearance → Theme File Editor, or an FTP client / file manager.
  • A backup of your site (always recommended before editing code).
  • Optionally, a child theme to avoid losing changes when the parent theme updates.

Step-by-Step

  1. Decide who to block. Identify the user roles you want to restrict. Common choices: Subscriber, Contributor, or Author. You can also target specific users by user ID or username.
  2. Choose your method. You have two main options: a code snippet (more control, no extra plugin) or a plugin like “WP Admin Block” or “Adminimize” (easier for non-coders). This guide focuses on the code method.
  3. Access your theme's functions.php. In your WordPress admin, go to Appearance → Theme File Editor. Select functions.php from the list on the right. If you're using a child theme, edit the child theme's functions.php. Alternatively, connect via FTP and open the file in a text editor.
  4. Add the redirect code. Paste the following snippet at the end of the file, before the closing ?> tag (if present). This code redirects users with the 'subscriber' role away from the dashboard to the home page, unless they are an administrator or are doing an AJAX request (needed for some plugins).
// Limit dashboard access to admins (and optionally editors)
add_action( 'init', 'limit_dashboard_access' );
function limit_dashboard_access() {
    // Allow admin and editor roles
    if ( current_user_can( 'edit_pages' ) ) {
        return;
    }
    // Allow AJAX requests (for plugin functionality)
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        return;
    }
    // If the user is trying to access wp-admin or admin-ajax.php
    if ( is_admin() && ! wp_doing_cron() ) {
        wp_redirect( home_url() );
        exit;
    }
}

Note: Replace edit_pages with the capability you want to allow. For example, edit_posts for authors, or read for everyone (then remove the check). To block specific users instead of roles, use current_user_can( 'manage_options' ) or check user ID.

  1. Save the file. Click “Update File” in the theme editor, or save via FTP. Test by logging in as a subscriber (use a private/incognito window) and trying to visit /wp-admin. You should be redirected to the home page.
  2. Hide the admin bar (optional). By default, blocked users still see the admin bar on the front end. To hide it for the same roles, add this to the same functions.php:
// Hide admin bar for non-admins
add_action( 'after_setup_theme', 'hide_admin_bar_for_non_admins' );
function hide_admin_bar_for_non_admins() {
    if ( ! current_user_can( 'manage_options' ) ) {
        show_admin_bar( false );
    }
}
  1. Test thoroughly. Check that the redirect works for all blocked roles, but that admins and editors can still access the dashboard. Also test that front-end functionality (like commenting) still works for blocked users.

Common Mistakes

  • Blocking yourself out. If you accidentally block the admin role, you'll lose access to the dashboard. Always test with a non-admin user first.
  • Using the wrong capability. Capabilities are hierarchical; edit_pages is for editors and above, edit_posts for authors and above. If you use a capability that's too high, you'll block users you didn't intend to.
  • Forgetting AJAX. Many plugins use AJAX calls to admin-ajax.php. If you block all admin access, those calls will fail, breaking things like front-end forms or live search. The code above excludes AJAX requests to avoid this.
  • Editing the parent theme directly. If you add the code to your main theme and then update the theme, your changes will be lost. Use a child theme or a site-specific plugin.

Tips

  • Use a site-specific plugin. Instead of editing functions.php, create a small custom plugin. That way, your code survives theme changes. You can use a plugin like “Code Snippets” to add the snippet without creating a file.
  • Redirect to a custom page. Instead of the home page, you can redirect to a page like “Access Denied” or your login page. Change home_url() to wp_redirect( site_url( '/login' ) ) or any URL.
  • Use a plugin for non-coders. If you're uncomfortable with code, use a dedicated plugin like “WP Admin Block” or “Adminimize”. They offer a UI to select which roles to block.
  • Consider user experience. If you block authors, they can't see the “Edit” links on the front end. That's often desired, but make sure they know how to submit content (e.g., via a front-end form).

Troubleshooting

I'm locked out of the dashboard. If you accidentally blocked yourself, you can regain access via FTP or your hosting file manager. Open functions.php and remove the code, or rename the plugin folder if you used a plugin. Alternatively, use a database tool like phpMyAdmin to change your user role to administrator directly in the wp_usermeta table.

The redirect doesn't work. Check that you've saved the file correctly and that there are no PHP syntax errors. Also, clear any caching plugins (like W3 Total Cache) that might be serving a cached version of the page. If you're using a page builder, it might have its own admin access rules.

AJAX requests are failing. Make sure your code includes the DOING_AJAX check. If you're still having issues, check your browser's console for 403 errors. You may need to allow admin-ajax.php for all logged-in users.

Users can still access the dashboard via a direct URL. Some plugins or themes might override your redirect. Try adding the redirect later in the loading process, e.g., on admin_init instead of init. Also, check if any security plugin is interfering.

Frequently Asked Questions

Q: Can I block a specific user instead of a role?
A: Yes. Instead of checking capabilities, check the user ID. For example: if ( get_current_user_id() === 5 ) { wp_redirect( home_url() ); exit; }

Q: Will this affect the REST API?
A: No, the REST API is separate from the admin dashboard. Your code only redirects wp-admin URLs. If you want to restrict REST API access, you'll need additional code.

Q: Can I still let users edit their profiles?
A: Yes, you can allow access to the profile page by adding a condition: if ( isset( $_GET['profile'] ) ) or by checking the request URI. But it's simpler to use a plugin that offers granular control.

Q: Does this work for multisite?
A: Yes, but be careful. The code will apply to all sites in the network. You may need to adjust the capability checks based on super admin status.

Q: Will this slow down my site?
A: No, the code is lightweight and runs only on admin requests. It has minimal impact on performance.

Q: Can I use this to block access to the customizer?
A: The customizer is part of the admin, so it will be blocked as well. If you need to allow access to the customizer for certain roles, you'll need a more complex solution.