Created
July 2, 2018 09:49
-
-
Save kartick14/a0fc8a978fc59b11439768304122cc33 to your computer and use it in GitHub Desktop.
Wordpress login restriction depends on user role.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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