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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.