Skip to content

Instantly share code, notes, and snippets.

@kartick14
Created July 2, 2018 09:49
Show Gist options
  • Save kartick14/a0fc8a978fc59b11439768304122cc33 to your computer and use it in GitHub Desktop.
Save kartick14/a0fc8a978fc59b11439768304122cc33 to your computer and use it in GitHub Desktop.
Wordpress login restriction depends on user role.
<?php add_filter( 'wp_authenticate_user', 'restrict_login', 10, 2 );
function restrict_login( $user, $password ) {
if ( is_wp_error( $user ) ) {
return $user;
}
$roles = array('administrator','subscriber');
//$roles[] = 'administrator';
if (array_reduce( $roles, function ( $allowed, $role ) use ( $user ) {
return $allowed || user_can( $user, $role );
}, false )
) {
return $user;
}
return new WP_Error( 'auth', __( 'Access denied!', 'restrict-role-login' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment