SNIPPET · FREE
Block Subscriber access to wp-admin
Users need to log in, but not access the dashboard? This snippet redirects Subscribers to the homepage when they try to open /wp-admin/.
The code
AJAX requests continue to work normally. Users who can edit content are not blocked, even if they also have the Subscriber role.
/**
* Redirect WordPress Subscribers away from wp-admin.
*/
add_action( 'admin_init', function () {
if ( wp_doing_ajax() ) {
return;
}
$user = wp_get_current_user();
if ( ! $user || ! $user->exists() ) {
return;
}
$is_subscriber = in_array( 'subscriber', (array) $user->roles, true );
$can_edit = current_user_can( 'edit_posts' );
if ( $is_subscriber && ! $can_edit ) {
wp_safe_redirect( home_url( '/' ) );
exit;
}
} );
This is a dashboard UX redirect, not a security boundary. It does not protect public content or replace WordPress capabilities.
How to use it
- Install Code Snippets Free in WordPress.
- Create a new PHP snippet.
- Copy and paste the code.
- Activate it in the required scope.
This is a simple option, not a requirement: experienced users can put the code in their own plugin or the appropriate location. Configure form IDs and any required providers before enabling Gravity Forms examples.