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.
Why Monitor Admin Logins?
Enhance your WordPress site’s security by monitoring admin login activity. By adding a short code to your theme’s functions.php
file, you can receive email notifications whenever an administrator logs in. This helps you promptly detect unauthorized access while ensuring minimal impact on website performance.
How to Implement Code for Admin Login Notifications
If your website or server does not have a mail-sending function, you can refer to the setup guides:
- How to Use SMTP to Send Emails From WordPress without a Plugin
- Setting up email sending using Gmail SMTP on Ubuntu/Debian
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 || !is_a($user, 'WP_User')) { return; } if (user_can($user, 'manage_options')) { $admin_email = get_option('admin_email'); $cc_email = 'youmail@domain.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; wp_mail($admin_email, $subject, nl2br($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.$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.
You can find more detailed information on WordPress support sites:
A simple and effective way to protect your website from unauthorized access is by using custom code to send notification emails when an admin logs into WordPress. Don’t forget to customize the email content and back up your website before making any changes.