The invited_user_email WordPress PHP Filter allows you to modify the contents of the email sent when an existing user is invited to join the site.
Usage
add_filter('invited_user_email', 'customize_invited_user_email', 10, 7);
function customize_invited_user_email($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
// your custom code here
return $new_user_email;
}
Parameters
$new_user_email(array): Used to build wp_mail().$to(string): The email address of the invited user.$subject(string): The subject of the email.$message(string): The content of the email.$headers(string): Headers.$user_id(int): The invited user’s ID.$role(array): Array containing role information for the invited user.$newuser_key(string): The key of the invitation.
More information
See WordPress Developer Resources: invited_user_email
Examples
Customize the subject and message of the invitation email
Modify the email subject and content to make it more personalized.
add_filter('invited_user_email', 'customize_invitation_email', 10, 7);
function customize_invitation_email($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
$user = get_userdata($user_id);
$subject = "Welcome, {$user->display_name}! You're invited to join our awesome site!";
$message = "Hi {$user->display_name},\n\nWe'd love to have you on board. Please accept our invitation to join our site.\n\nBest regards,\nThe Team";
$new_user_email['subject'] = $subject;
$new_user_email['message'] = $message;
return $new_user_email;
}
Add a custom header to the invitation email
Add a custom header to the email, such as a “Reply-To” address.
add_filter('invited_user_email', 'add_reply_to_header', 10, 7);
function add_reply_to_header($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
$headers .= "Reply-To: [email protected]\r\n";
$new_user_email['headers'] = $headers;
return $new_user_email;
}
Change the “To” email address
Send the invitation email to a different email address than the invited user.
add_filter('invited_user_email', 'change_to_email', 10, 7);
function change_to_email($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
$to = "[email protected]";
$new_user_email['to'] = $to;
return $new_user_email;
}
Add a custom footer to the invitation email
Add a custom footer to the email content.
add_filter('invited_user_email', 'add_email_footer', 10, 7);
function add_email_footer($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
$footer = "\n\n---\nThis is a custom footer for the invitation email.";
$message .= $footer;
$new_user_email['message'] = $message;
return $new_user_email;
}
Change the user role in the email message
Modify the user role mentioned in the email content.
add_filter('invited_user_email', 'change_role_in_email', 10, 7);
function change_role_in_email($new_user_email, $to, $subject, $message, $headers, $user_id, $role, $newuser_key) {
$custom_role = "Super Contributor";
$message = str_replace($role['name'], $custom_role, $message);
$new_user_email['message'] = $message;
return $new_user_email;
}