ulgm_mail_headers
Filters the email headers before sending, allowing modification of mail metadata.
add_filter( 'ulgm_mail_headers', $callback, 10, 1 );
Description
Filters the email headers before they are sent. Developers can use this hook to modify or add custom headers to outgoing emails, useful for setting specific email client behaviors or tracking information. The `$headers` parameter contains the current email headers.
Usage
add_filter( 'ulgm_mail_headers', 'your_function_name', 10, 1 );
Parameters
-
$headers(mixed) - This parameter contains the email headers that will be sent with the email.
Return Value
The filtered value.
Examples
/**
* Example function to modify email headers for the ulgm_mail_headers filter.
* This function might add a custom header or modify an existing one.
*
* @param array|string $headers The email headers.
* @return array|string The modified email headers.
*/
add_filter( 'ulgm_mail_headers', 'my_custom_ulgm_mail_headers', 10, 1 );
function my_custom_ulgm_mail_headers( $headers ) {
// Ensure $headers is an array for easier manipulation.
// If it's already an array, use it directly. If it's a string, convert it.
if ( is_string( $headers ) ) {
// A common format for string headers is newline-separated key: value pairs.
// For simplicity, we'll assume it's one header and treat it as an array.
// In a real scenario, you'd parse it more robustly.
$headers = array( $headers );
} elseif ( ! is_array( $headers ) ) {
// If it's neither string nor array, initialize it as an empty array.
$headers = array();
}
// Example: Add a custom Reply-To header.
// In a real application, you might fetch this from theme options or user meta.
$reply_to_email = '[email protected]';
$reply_to_name = 'Example Support Team';
// Check if Reply-To header already exists and update it, or add it.
$reply_to_found = false;
foreach ( $headers as $key => $header ) {
if ( stripos( $header, 'Reply-To:' ) === 0 ) {
$headers[ $key ] = "Reply-To: {$reply_to_name} <{$reply_to_email}>";
$reply_to_found = true;
break;
}
}
if ( ! $reply_to_found ) {
$headers[] = "Reply-To: {$reply_to_name} <{$reply_to_email}>";
}
// Example: Add a custom X-Mailer header.
$headers[] = 'X-Mailer: My Custom WordPress Mailer';
return $headers;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/shared-functions.php:918
public static function wp_mail( $to, $subject, $body, $headers = array(), $attachment = array() ) {
$body = do_shortcode( $body );
$subject = do_shortcode( $subject );
$to = apply_filters( 'ulgm_mail_to', $to );
$subject = apply_filters( 'ulgm_mail_subject', html_entity_decode( $subject ) );
$body = apply_filters( 'ulgm_mail_body', html_entity_decode( $body ) );
$headers = apply_filters( 'ulgm_mail_headers', $headers );
$attachment = apply_filters( 'ulgm_mail_attachment', $attachment );
return wp_mail( $to, $subject, $body, $headers, $attachment );
}