[ad_1]
hello, so I have a custom code that works when Redirect After Login is OFF. The code notifies admin of a successful/unsuccessful login event. The problem is that when Redirect After Login is ON and a user logs in successfully then the function DOES NOT TRIGGER the email to be sent. It has something to do with Redirect After Login being ON. (when OFF it all works fine)
Below I attached the code that normally works on all other websites but breaks with Redirect After Login feature being ON.
// Add a hook to execute the function when a user logs in
add_action('wp_login', 'send_login_notification', 10, 2);
// Add a hook to execute the function when a login attempt fails
add_action('wp_login_failed', 'send_login_failed_notification', 5, 1);
function send_login_notification($user_login, $user) {
$admin_email = get_option('admin_email');
$subject="New Login on " . get_bloginfo('name');
$message="A user has logged in to the website:" . "\n\n";
$message .= 'Username: ' . $user_login . "\n";
$message .= 'User ID: ' . $user->ID . "\n";
$message .= 'User IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$message .= 'User Agent: ' . $_SERVER['HTTP_USER_AGENT'] . "\n";
// Send the email
wp_mail($admin_email, $subject, $message);
}
function send_login_failed_notification($username) {
$admin_email = get_option('admin_email');
$subject="Login Failed on " . get_bloginfo('name');
$message="A login attempt failed on the website:" . "\n\n";
$message .= 'Username: ' . $username . "\n";
$message .= 'User IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$message .= 'User Agent: ' . $_SERVER['HTTP_USER_AGENT'] . "\n";
// Send the email
wp_mail($admin_email, $subject, $message);
}
