# Block Logins From Accounts Without a Role

> Stops accounts that have no role on this site (often left over after a role was removed) from logging in, which closes a common gap on older sites.

- Type: PHP
- Runs: Run everywhere
- Category: [Login](https://wpide.com/library/category/login/)
- Tags: login, roles, security
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/block-login-without-role/

## Code

```php
add_filter( 'authenticate', function ( $user ) {
    if ( ! $user instanceof WP_User || is_super_admin( $user->ID ) ) {
        return $user;
    }

    if ( empty( $user->roles ) ) {
        return new WP_Error( 'wpide_no_role', '<strong>Error:</strong> Your account does not have access to this site.' );
    }

    return $user;
}, 60 );
```

## Notes

On multisite, network super admins are always allowed through.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Block Logins From Accounts Without a Role" 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/
