Would you like to receive a notification email whenever an administrator account logs in to WordPress? This is particularly useful when you want to monitor accounts such as authors, editors, or other high-privileged users on your website, helping you manage administrative accounts and website security.
How to Send Email Notifications for Admin Login in WordPress
Purpose of Receiving Notifications for Administrator Login:
- Enhance the security of your WordPress site by monitoring admin login activity.
- Receive email notifications when an administrator logs in, helping you promptly detect unauthorized access.
Benefits of Sending Login Notification Emails to Admin
- Easy Implementation: Simply add a short code to your WordPress theme’s functions.php file.
- Improved Efficiency: Receive timely notification emails whenever an administrator logs in.
- Minimal Impact: Does not affect website performance.
Steps:
Note: To use this feature, your website must be configured to send emails using SMTP. If you encounter difficulties setting up your SMTP email settings, you can refer to We’s article on using custom code for SMTP email sending in WordPress.
Step 1: Access the functions.php file:
- Use FTP or a file manager to access the
/wp-content/themes/
. - Open the file
functions.php
of the WordPress theme you are using.
Step 2: Add the code below at the end of the file
add_action('wp_login', 'send_admin_login_email', 10, 2);
function send_admin_login_email($user_login, $user) {
if (user_can($user, 'manage_options')) {
$admin_email = 'arriveddev.bz@gmail.com';
$cc_email = get_option('admin_email');
$cc_email_additional = 'admin@arriveddev.com';
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_browser = $_SERVER['HTTP_USER_AGENT'];
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
$subject = 'User '. $user_login . ' with role [' . $user_role . '] logged in';
$message = 'Admin ' . $user_login . ' logged in to the website at ' . date('Y-m-d H:i:s') . ".\n";
$message .= 'IP Address: ' . $user_ip . ".\n";
$message .= 'Browser: ' . $user_browser;
$headers = array('Content-Type: text/html; charset=UTF-8');
$headers[] = 'Cc: ' . $cc_email . ',' . $cc_email_additional;
wp_mail($admin_email, $subject, $message, $headers);
}
}
Note:
- Replace
$admin_email
with the email address you want to receive notifications from. - The above code is just an example and may need to be adjusted to fit your site’s structure.
Step 3: Save the functions.php file
- After adding the code, save the
functions.php
.
Step 4: Check and confirm email sending has worked:
- Log in to your WordPress admin to enable notifications.
- Check your inbox for email notifications when you sign in . (Like We’s screenshot below).
Code snippet explanation:
$user_login
: Username of the user logging into the website.$user
: User object, containing user login information such as name, email, role, etc$admin_email
: The email address of the admin who receives notifications about logging in.$cc_email
: Email address attached in the “cc” field of the notification email.$cc_email_additional
: Additional email address attached in the “cc” field of the notification email.$user_ip
: IP address of the logged in user.$user_browser
: Information about the browser the logged in user is using.$user_roles
: An array containing user roles on the website.$user_role
: User role, retrieved from array$user_roles
.$subject
: Title of the notification email, including the user’s name and their role.$message
: Content of the notification email, including information about the user’s login such as time, IP address and browser.$headers
: An array containing email headers such as “Content-Type” and “Cc” to send notification emails.
A simple and effective way to safeguard your website from unauthorized access is by using custom code to trigger notification emails upon admin login in WordPress. Don’t forget to customize the email information and create a website backup before making any changes.