SNIPPET · FREE

WordPressAccess

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.

PHP · WordPress · Code Snippets compatible
/**
 * 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

  1. Install Code Snippets Free in WordPress.
  2. Create a new PHP snippet.
  3. Copy and paste the code.
  4. 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.