function send_login_email($user_login, $user) {
$to = 'your_email@dot.com';
$subject = $user_login . ' user has logged in to your WordPress Dashboard';
$message = 'Your WordPress was accessed by user' . $user_login . '
';
$message .= 'IP: ' . $_SERVER['REMOTE_ADDR'] . '
';
$message .= 'Country: ' . get_country_from_ip($_SERVER['REMOTE_ADDR']) . '
';
$message .= 'Time: ' . current_time('mysql') . '
';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $message, $headers );
}
add_action('wp_login', 'send_login_email', 10, 2);
function get_country_from_ip($ip_address) {
$url = 'https://ipapi.co/' . $ip_address . '/country_name/';
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return '';
} else {
$country_name = wp_remote_retrieve_body($response);
return $country_name;
}
}