Using WordPress ‘comment_notification_subject’ PHP filter

The comment_notification_subject WordPress PHP filter allows you to modify the subject of the comment notification email sent to the post author.

Usage

add_filter('comment_notification_subject', 'your_custom_function', 10, 2);

function your_custom_function($subject, $comment_id) {
    // your custom code here
    return $subject;
}

Parameters

  • $subject (string): The comment notification email subject.
  • $comment_id (string): Comment ID as a numeric string.

More information

See WordPress Developer Resources: comment_notification_subject

Examples

Add a prefix to the email subject

Add a custom prefix to the comment notification email subject.

add_filter('comment_notification_subject', 'add_prefix_to_subject', 10, 2);

function add_prefix_to_subject($subject, $comment_id) {
    $subject = '[My Blog] ' . $subject;
    return $subject;
}

Add the post title to the email subject

Include the post title in the comment notification email subject.

add_filter('comment_notification_subject', 'add_post_title_to_subject', 10, 2);

function add_post_title_to_subject($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $post = get_post($comment->comment_post_ID);
    $subject = $subject . ' - ' . $post->post_title;
    return $subject;
}

Change the email subject based on comment type

Modify the comment notification email subject based on the type of the comment (e.g., trackback or pingback).

add_filter('comment_notification_subject', 'change_subject_based_on_type', 10, 2);

function change_subject_based_on_type($subject, $comment_id) {
    $comment = get_comment($comment_id);
    if ($comment->comment_type == 'trackback') {
        $subject = 'New trackback on your post';
    } elseif ($comment->comment_type == 'pingback') {
        $subject = 'New pingback on your post';
    }
    return $subject;
}

Add the commenter’s name to the email subject

Include the name of the person who left the comment in the comment notification email subject.

add_filter('comment_notification_subject', 'add_commenter_name_to_subject', 10, 2);

function add_commenter_name_to_subject($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $subject = $subject . ' by ' . $comment->comment_author;
    return $subject;
}

Remove the blog name from the email subject

Remove the blog name from the comment notification email subject.

add_filter('comment_notification_subject', 'remove_blog_name_from_subject', 10, 2);

function remove_blog_name_from_subject($subject, $comment_id) {
    $blog_name = sprintf( __('[%1$s]'), wp_specialchars_decode(get_option('blogname'), ENT_QUOTES) );
    $subject = str_replace($blog_name, '', $subject);
    return $subject;
}