Using WordPress ‘comment_notification_text’ PHP filter

The comment_notification_text WordPress PHP filter allows you to modify the comment notification email text.

Usage

add_filter('comment_notification_text', 'custom_comment_notification_text', 10, 2);

function custom_comment_notification_text($notify_message, $comment_id) {
    // your custom code here
    return $notify_message;
}

Parameters

  • $notify_message (string) – The original comment notification email text.
  • $comment_id (string) – The comment ID as a numeric string.

More information

See WordPress Developer Resources: comment_notification_text

Examples

Customize the email text

Modify the email text for comment notifications.

add_filter('comment_notification_text', 'change_email_text', 10, 2);

function change_email_text($notify_message, $comment_id) {
    $notify_message = "A new comment has been posted on your website:\n\n";
    return $notify_message;
}

Include the author’s name in the email text

Add the author’s name to the comment notification email.

add_filter('comment_notification_text', 'add_author_name', 10, 2);

function add_author_name($notify_message, $comment_id) {
    $comment = get_comment($comment_id);
    $author_name = $comment->comment_author;
    $notify_message = "Author: {$author_name}\n\n" . $notify_message;
    return $notify_message;
}

Add a custom footer to the comment notification email.

add_filter('comment_notification_text', 'add_email_footer', 10, 2);

function add_email_footer($notify_message, $comment_id) {
    $footer = "\n\nThank you for using our website!";
    $notify_message .= $footer;
    return $notify_message;
}

Replace a specific word in the email text

Replace a specific word in the comment notification email.

add_filter('comment_notification_text', 'replace_word_in_email', 10, 2);

function replace_word_in_email($notify_message, $comment_id) {
    $notify_message = str_replace('website', 'blog', $notify_message);
    return $notify_message;
}

Add a link to the comment in the comment notification email.

add_filter('comment_notification_text', 'add_comment_link', 10, 2);

function add_comment_link($notify_message, $comment_id) {
    $comment_link = get_comment_link($comment_id);
    $notify_message .= "\n\nView the comment: {$comment_link}";
    return $notify_message;
}