Last active
June 5, 2024 16:22
-
-
Save dartiss/9a7fda927ebf7ecc56884b4bb8871d4a to your computer and use it in GitHub Desktop.
Disable Email Login for WP-Admin
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 | |
remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 ); | |
/** | |
* Disable logging in via email | |
* | |
* Check for and serve an appropriate response to users attempting to sign in with email. | |
* | |
* @param string $user If the user is authenticated. | |
* @param string $username Username or email address. | |
* @return string Authentication details | |
*/ | |
function disable_email_login( $user, $username ) { | |
if ( ! empty( $username ) && filter_var( $username, FILTER_VALIDATE_EMAIL ) ) { | |
return new WP_Error( 'email_login_disabled', __( 'Logging in with email is disabled.', 'text-domain' ) ); | |
} | |
return $user; | |
} | |
add_filter( 'authenticate', 'disable_email_login', 20, 3 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script created by my colleague @f0rthelulz, for a WordPress VIP customer.
Can be added to
functions.php
and will then prevent an email address from being used to login - usernames only can be used. Just changetext-domain
to, well, your text domain for the error translation to work.