您可以添加一个低优先级的身份验证挂钩,该挂钩在所有其他挂钩之后运行,并拒绝特定用户ID的身份验证:
function wpse_384212_ban_user_id( $user, $username, $password ) {
// At this point $user is either a WP_User object if they\'ve successfully logged in,
// or null or a WP_Error if they haven\'t. We want to reject users by ID.
if ( $user instanceof WP_User ) {
// This is a successfully authenticated user. Check their ID.
$banned_user_ids = array( 3, 6, 10 ); // could also store these in a site option
if ( in_array( $user->ID, $banned_user_ids ) ) {
return new WP_Error( \'user_banned\', \'You are banned\' );
}
}
// Else return the user or error from previous authentication filters
return $user;
}
add_filter( \'authenticate\', \'wpse_384212_ban_user_id\', 999, 3 );
这将拒绝以其他方式成功登录的用户。如果您想拒绝他们,无论他们是否成功登录,您需要查找用户以获取他们的ID以进行额外检查:
function wpse_384212_ban_user_id( $user, $username, $password ) {
// At this point $user is either a WP_User object if they\'ve successfully logged in,
// or null or a WP_Error if they haven\'t. We want to reject users by ID.
$banned_user_ids = array( 3, 6, 10 ); // could also store these in a site option
if ( $user instanceof WP_User ) {
// This is a successfully authenticated user. Check their ID.
if ( in_array( $user->ID, $banned_user_ids ) ) {
return new WP_Error( \'user_banned\', \'You are banned\' );
}
} elseif ($username) {
// Unsuccessful login but we have a username.
// Look up the user by username or email to see if they are a banned user
$login_user = get_user_by( \'login\', $username );
if ( ! $login_user && is_email( $username ) ) {
$login_user = get_user_by( \'email\', $username );
}
if ( $login_user instanceof WP_User ) {
// This was a failed login for a valid user. Check the user\'s ID
if ( in_array( $login_user->ID, $banned_user_ids ) ) {
return new WP_Error( \'user_banned\', \'You are banned\' );
}
}
}
// Else return the user or error from previous authentication filters
return $user;
}
add_filter( \'authenticate\', \'wpse_384212_ban_user_id\', 999, 3 );
如果这是一个多站点,那么这些将需要一个mu插件。