Support » How-To and Troubleshooting » Send email at failed login using wp_login_failed

Send email at failed login using wp_login_failed

  • Hello everyone, this is my first post here !

    I am trying to make a function that automatically sends an email to a specified address when a failed login attempt occurs using the wp_login_failed hook but some reason I can’t make it work and cannot figure out why.

    I tried with this code in my theme’s functions.php file

    function email_at_login_failed( $username ) {
         wp_mail('me@example.net', 'Failed login attempt', 'Failed login message');

    I does not work, I get no email. I read that wp_failed_login only fires up if both a wrong username and password combination is entered.

    Email works ok, I tried adding just wp_mail('me@example.net', 'Failed login attempt', 'Failed login message'); and it works.

    Sorry for my bad english !

Viewing 5 replies - 1 through 5 (of 5 total)
  • add_action('wp_login_failed','email_at_login_failed');
    function email_at_login_failed( $username ) {
        wp_mail('myrealmail@myrealdomain.com', 'Failed login attempt by '.$username, 'Failed login message');

    Just tried it on a clean install with Twentytwelve theme. This code works perfectly.. Just needed to remove the last curlybracket. I entered only a wrong password and I received the email.

    You could btw use this variable for the email receiver
    <?php $admin_email = get_settings('admin_email'); ?>

    It was that damn curly bracket, I feel stupid now :).

    I do receive emails now, but only if both username and password are filled, interesting that you get emails by only filling the password field.

    So my code is

    function email_at_login_failed( $username ) {
        $admin_email = get_settings('admin_email');
        $ip = $_SERVER['REMOTE_ADDR'];
        wp_mail($admin_email, 'Failed login attempt', 'Failed login with username' . ' ' .$username . 'and IP ' .$ip);

    This function sends an email containing the (wrong) username entered and the IP of the person’s computer to the admin’s email address entered in WP > Settings > General.

    I will work on it adding more useful info and post here.

    Thank you very much curlybracket

    Well, it works but today I noticed that I get about 30 – 40 emails at once each having the same timestamp, so something isn’t right.

    Could be somehow related to W3 total cache plugin ? Or Cloudflare ?

    Hello, glad it works!

    I don’t know anything about cloudflare.

    And could this be related to cache ?
    Well.. i am not sure. Cache would probably not recall a _POST variable ?

    It seem it not related to either W3Total Cache plugin or Cloudflare because I tried the function on installations that have none of the above. It seems to be random, I get bursts of 30 – 90 emails with failed attempts, same IP, same timestamp and message.

    Is there another way to do this ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Send email at failed login using wp_login_failed’ is closed to new replies.
Skip to toolbar