USP Pro provides robust options for Email Alerts (email notifications). For most users, the plugin settings provide everything needed for customizing and dialing in the perfect set of alerts. If necessary, it’s also possible to go beyond the settings and further customize things like email headers and other details. This tutorial explains how to make it happen with a bit of custom code.
Step 1: Add the code
To customize email alerts, headers and other details, add the following code to your WordPress site.
function usp_get_email_vars($vars, $action, $charset, $mail_format) {
$headers = isset($vars['headers']) ? $vars['headers'] : null;
$admin_name = isset($vars['admin_name']) ? $vars['admin_name'] : null;
$admin_from = isset($vars['admin_from']) ? $vars['admin_from'] : null;
if ($headers && $admin_name && $admin_from) {
$headers = 'X-Mailer: USP Pro'. "\n";
$headers .= 'From: '. $admin_name .' <'. $admin_from .'>'. "\n";
$headers .= 'Reply-To: '. $admin_name .' <'. $admin_from .'>'. "\n";
if ($mail_format == 'html') {
$headers .= 'Content-Type: text/html; charset='. $charset . "\n";
} else {
$headers .= 'Content-Type: text/plain; charset='. $charset . "\n";
}
$vars['headers'] = $headers;
}
return $vars;
}
add_filter('usp_get_email_vars', 'usp_get_email_vars', 10, 4);
As written, the above code does not make any changes to email alerts; the returned variables $vars
all use the default values. This step is just to get familiar with the required code. So now, to go ahead and customize email headers and other details, proceed to the next step..
Step 2: Customize
By modifying the previous code (from step 1), it’s possible to customize just about any aspect of email alerts. As an example, say we want to replace the Reply To
header with the user’s submitted email address. This would be useful if you want to send an email to the user by simply replying to the email alert message. Here is what the modified code looks like:
function usp_get_email_vars($vars, $action, $charset, $mail_format) {
$headers = isset($vars['headers']) ? $vars['headers'] : null;
$admin_name = isset($vars['admin_name']) ? $vars['admin_name'] : null;
$admin_from = isset($vars['admin_from']) ? $vars['admin_from'] : null;
$user_email = isset($vars['user_email']) ? $vars['user_email'] : null;
$user_email = $user_email ? $user_email : $admin_from;
if ($headers && $admin_name && $admin_from && $user_email) {
$headers = 'X-Mailer: USP Pro'. "\n";
$headers .= 'From: '. $admin_name .' <'. $admin_from .'>'. "\n";
$headers .= 'Reply-To: '. $user_email .' <'. $user_email .'>'. "\n";
if ($mail_format == 'html') {
$headers .= 'Content-Type: text/html; charset='. $charset . "\n";
} else {
$headers .= 'Content-Type: text/plain; charset='. $charset . "\n";
}
$vars['headers'] = $headers;
}
return $vars;
}
add_filter('usp_get_email_vars', 'usp_get_email_vars', 10, 4);
That code replaces the default Reply To
header with the user’s submitted email address. If no address is submitted, falls back to the admin address specified in Admin settings. This is just one example to give you an idea of how to get in there and customize things as needed. Important notes below..
From
header, will result in alerts getting flagged and sent to spam, etc.$action
variable can be scheduled
, denied
, approved
, or submit
, so we can target specific types of email alerts.