# Keep Subscribers Out of wp-admin

> Redirects users who cannot edit posts (subscribers, customers) to the homepage when they try to open the admin area. AJAX and admin-post requests keep working.

- Type: PHP
- Runs: Admin only
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: redirects, admin, subscribers, roles
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-non-editors-from-admin/

## Code

```php
add_action( 'admin_init', function () {
    global $pagenow;

    if ( wp_doing_ajax() || 'admin-post.php' === $pagenow || current_user_can( 'edit_posts' ) ) {
        return;
    }

    wp_safe_redirect( home_url( '/' ) );
    exit;
} );
```

## Notes

Users can still change their password through the front-end account page if your site has one.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Keep Subscribers Out of wp-admin" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
