How to Send Email Notifications to Site Admin and Users on User Role Change in WordPress

In this tutorial, you will learn how to set up a WordPress functionality that sends email notifications to the site administrator and users whenever a user’s role is changed on the website. This feature significantly enhances site management, ensuring administrators are promptly informed about user role modifications, while also keeping users in the loop about their account changes. We will achieve this without relying on plugins, using custom WordPress code snippets. Let’s dive into the details of how you can implement this feature for your WordPress site.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Edit Your Child Theme’s functions.php

To begin, open your WordPress child theme’s functions.php file. This file serves as the central hub for customizing your theme’s features. You can edit it directly within the Theme Editor in your WordPress dashboard or use an external code editor in combination with FTP (File Transfer Protocol) for more advanced editing. Insert the provided code snippet at the file’s end to implement the desired functionality.

Code Snippet 1: Send Email Notification to Admin

function send_admin_email_on_role_change( $user_id, $new_role ) {
	$user          = get_userdata( $user_id );
	$username      = $user->user_login; // Get the username
	$admin_email   = get_option( 'admin_email' ); // Get the site admin email address
	$admin_subject = 'User Role Change Notification for ' . $username . ' on ' . get_bloginfo( 'name' );
	$admin_message = 'Hello Admin, the user role for ' . $user->display_name . ' (ID: ' . $user_id . ') has changed to ' . $new_role;

	// Send email to admin
	wp_mail( $admin_email, $admin_subject, $admin_message );
}

add_action( 'set_user_role', 'send_admin_email_on_role_change', 10, 2 );
Code language: PHP (php)

Explanation:

  • The send_admin_email_on_role_change function constructs an email with information about the user whose role has changed and sends it to the site administrator.
  • wp_mail() function sends the email to the specified admin email address.

Code Snippet 2: Send Email Notification to User

function send_user_email_on_role_change( $user_id, $new_role ) {
	$user         = get_userdata( $user_id );
	$username     = $user->user_login; // Get the username
	$user_email   = $user->user_email;
	$user_subject = 'Your Role Change Notification on ' . get_bloginfo( 'name' ) . ' (Username: ' . $username . ')';
	$user_message = 'Hello ' . $user->display_name . ', your role has changed to ' . $new_role;

	// Send email to user
	wp_mail( $user_email, $user_subject, $user_message );
}

add_action( 'set_user_role', 'send_user_email_on_role_change', 10, 2 );
Code language: PHP (php)

Explanation:

  • The send_user_email_on_role_change function constructs an email with information about the user’s new role and sends it to the user.
  • wp_mail() function sends the email to the specified user email address.

Advanced Email Notification for Admin (Optional)

In this example, we provide a more detailed email notification for administrators when a user’s role changes. This example includes additional user details and a visually appealing HTML-formatted email.

Code Snippet 3: Advanced Email Notification to Admin

function send_advanced_admin_email_on_role_change( $user_id, $new_role ) {
	$user                 = get_userdata( $user_id );
	$username             = $user->user_login;
	$user_email           = $user->user_email;
	$user_display_name    = $user->display_name;
	$user_registered_date = $user->user_registered;

	$admin_email   = get_option( 'admin_email' );
	$admin_subject = 'User Role Change Notification for ' . $username . ' on ' . get_bloginfo( 'name' );

	$admin_message  = '<html><body>';
	$admin_message .= '<h2>User Role Change Notification</h2>';
	$admin_message .= "<p>A user's role has been changed on your website:</p>";
	$admin_message .= '<ul>';
	$admin_message .= "<li><strong>Username:</strong> $username</li>";
	$admin_message .= "<li><strong>Display Name:</strong> $user_display_name</li>";
	$admin_message .= "<li><strong>Email:</strong> $user_email</li>";
	$admin_message .= "<li><strong>New Role:</strong> $new_role</li>";
	$admin_message .= "<li><strong>User Registered Date:</strong> $user_registered_date</li>";
	$admin_message .= '</ul>';
	$admin_message .= "<p>For more details, you can view the user's profile <a href='" . get_edit_user_link( $user_id ) . "'>here</a>.</p>";
	$admin_message .= '<p>Thank you for managing user roles on ' . get_bloginfo( 'name' ) . '!<br>Best regards,<br>Your Website Team</p>';
	$admin_message .= '</body></html>';

	// Headers for HTML email
	$headers[] = 'Content-Type: text/html; charset=UTF-8';

	// Send HTML email to admin
	wp_mail( $admin_email, $admin_subject, $admin_message, $headers );
}

add_action( 'set_user_role', 'send_advanced_admin_email_on_role_change', 10, 2 );
Code language: PHP (php)

Explanation:

The send_advanced_admin_email_on_role_change function constructs an HTML-formatted email with detailed information about the user whose role has changed and sends it to the site administrator.

wp_mail() function sends the HTML email to the specified admin email address.

Save Your functions.php Modifications and Test

Save the changes to your functions.php file. Now, whenever a user’s role changes, email notifications will be sent to both the site administrator and the user whose role has changed.

That’s it! You’ve successfully implemented a feature that sends email notifications to the site administrator and users whenever a user’s role changes on your WordPress website. This functionality enhances site management and user communication, ensuring both administrators and users are promptly informed about user role modifications.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

Your email address will not be published. Required fields are marked *