WooCommerce Login & Password Reset Filter

Disable & Obfuscate WooCommerce login and password reset attempts for admins.
mail@pastecode.io avatar
unknown
php
3 years ago
1.4 kB
11
Indexable
<?php 

function is_login_page() {
    return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}
add_filter('authenticate',function($user,$username) {
    if (!is_login_page()) {
        $user_by_username=get_user_by('login',$username);
        $user_by_email=get_user_by('email',$username);
        if ($user_by_username && in_array('administrator',$user_by_username->roles)) {
            return new WP_Error('authentication_failed', "<strong>Error:</strong> The username <strong>$username</strong> is not registered on this site. If you are unsure of your username, try your email address instead.");
        }
        else if ($user_by_email && in_array('administrator',$user_by_email->roles)) {
            return new WP_Error('authentication_failed', "Unknown email address. Check again or try your username.");
        }
    }
    return $user;
},100,2);

add_filter('allow_password_reset',function($allow,$user_id) {
    $user_by_id=get_user_by('id',$user_id);
    if ($user_by_id && in_array('administrator',$user_by_id->roles)) {
        if (!is_login_page()) {
            return new WP_Error('authentication_failed', "Invalid username or email.");
        }
        return new WP_Error('authentication_failed', "<strong>Error</strong>: There is no account with that username or email address.");
    }
    return $allow;
},100,2);