Using WordPress ‘comment_notification_headers’ PHP filter

The comment_notification_headers WordPress PHP filter modifies the headers of the comment notification email sent to the post author.

Usage

add_filter('comment_notification_headers', 'custom_comment_notification_headers', 10, 2);

function custom_comment_notification_headers($message_headers, $comment_id) {
    // your custom code here
    return $message_headers;
}

Parameters

  • $message_headers (string) – Headers for the comment notification email.
  • $comment_id (string) – Comment ID as a numeric string.

More information

See WordPress Developer Resources: comment_notification_headers

Examples

Adding a custom header to the comment notification email

To add a custom header, modify the $message_headers variable.

add_filter('comment_notification_headers', 'add_custom_header', 10, 2);

function add_custom_header($message_headers, $comment_id) {
    $message_headers .= "X-Custom-Header: CustomValue\r\n";
    return $message_headers;
}

Setting a different From address for the comment notification email

To change the From address, modify the From header in the $message_headers variable.

add_filter('comment_notification_headers', 'change_from_address', 10, 2);

function change_from_address($message_headers, $comment_id) {
    $message_headers = "From: Custom From Name <[email protected]>\r\n";
    return $message_headers;
}

Adding a Reply-To header to the comment notification email

To add a Reply-To header, modify the $message_headers variable.

add_filter('comment_notification_headers', 'add_reply_to_header', 10, 2);

function add_reply_to_header($message_headers, $comment_id) {
    $message_headers .= "Reply-To: [email protected]\r\n";
    return $message_headers;
}

Adding a custom header based on the comment ID

To add a custom header based on the comment ID, use the $comment_id variable.

add_filter('comment_notification_headers', 'add_custom_header_based_on_id', 10, 2);

function add_custom_header_based_on_id($message_headers, $comment_id) {
    $message_headers .= "X-Comment-ID: {$comment_id}\r\n";
    return $message_headers;
}

Changing the content type of the comment notification email

To change the content type, modify the Content-Type header in the $message_headers variable.

add_filter('comment_notification_headers', 'change_content_type', 10, 2);

function change_content_type($message_headers, $comment_id) {
    $message_headers = "Content-Type: text/html; charset=UTF-8\r\n";
    return $message_headers;
}