Get email when anyone logs in or tries to login WordPress dashboard

I have a site that needs to make more secured. So I am trying to get the notifications via email when any attempts(fails or success not matter) made to login into my site. I have tried below the codes.

<?php

function wpsln_log_wp_user_login( $user_user_login, $user ) {


    $admin_email = get_bloginfo('admin_email');
    $site_info = sprintf('%1$s (%2$s)', get_bloginfo('name'), get_bloginfo('wpurl'));

    // generate email core
    $header = 'From: "'.$admin_email.'" <'.$admin_email.'>'. "rn";
    $header .= "Content-type: text/html; charset: ".get_bloginfo('charset')."rn";
    $email_subject = sprintf(__('Login of the user %1$s on the website %2$s', 'wpsln'), $user->user_login, $site_info);

    $body_message = sprintf(__('Hello a user has logged in on the website %1$s. Here are the details of this access:', 'wpsln'),$site_info).'<br />'."n";
    $body_message .= sprintf(__('User email: %1$s', 'wpsln'),    $user->user_email).'<br />'."n";

    wp_mail($admin_email, $email_subject, $body_message, $header);

}
add_action( 'wp_login', 'wpsln_log_wp_user_login', '60', 2 );



function wpsln_log_wp_user_login_fail( $username ) {


    $site_info = sprintf(__('%1$s (%2$s)', 'wpsln'), get_bloginfo('name'), get_bloginfo('wpurl'));

    // generate email core
    $header = 'From: "'.$admin_email.'" <'.$admin_email.'>'. "rn";
    $header .= "Content-type: text/html; charset: ".get_bloginfo('charset')."rn";
    $email_subject = sprintf(__('/! Error : login failed on %1$s', 'wpsln'), $site_info);

    $body_message = sprintf(__('Hello, someone just failed to log in on %1$s. Here are the details:', 'wpsln'),$site_info).'<br />'."n";
    $body_message .= sprintf(__('Login: %1$s', 'wpsln'), $username).'<br />'."n";
    wp_mail($admin_email, $email_subject, $body_message, $header);

}
add_action( 'wp_login_failed', 'wpsln_log_wp_user_login_fail' );

But this code does not give me the notification. What are the problems I do not understand. Actually I am new in the WordPress. So please help me out.

Read More

Another ask to you. Here is the code may be for the notification to be sent to the admin email. In my site there is two more admin. My question is “to which admin email the notification email be sent?”

Thanks for reading this.

Related posts

2 comments

  1. You are trying right thing, but i think you are call wp_login hook very late.

    add_action( 'wp_login', 'wpsln_log_wp_user_login', '10', 2 );
    

    instead

    add_action( 'wp_login', 'wpsln_log_wp_user_login', '60', 2 );
    

    and your wp_login_failed works fine but you have missing

    $admin_email = get_bloginfo('admin_email');
    

    The mail will be send to administrator which is inside
    Settings->general->E-mail Address

  2. Why don’t you use Wordfence? It gives you details such as when the attempt was made, the ip address, country, and it allows you to block them as well. It also tells you who logged in and the users role. You also get a email notification every time the login fails or is successful. I usually set it to one attempt. This eliminates multiple attempts from the same ip and it blocks them from loggin in for at least 5 minutes. You can then permanently block them if you like. That’s what I usually do.

    I wait until they attempt to login, wordfence catches them, I see there ip addresses, then i ban them from the site.

    https://wordpress.org/plugins/wordfence/

Comments are closed.