Using WordPress ‘comment_moderation_subject’ PHP filter

The comment_moderation_subject WordPress PHP filter is used to modify the subject of the comment moderation email.

Usage

add_filter('comment_moderation_subject', 'my_custom_comment_moderation_subject', 10, 2);

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

Parameters

  • $subject (string): Subject of the comment moderation email.
  • $comment_id (int): Comment ID.

More information

See WordPress Developer Resources: comment_moderation_subject

Examples

Customizing the email subject

Modify the email subject by adding the site name and comment ID.

function my_custom_comment_moderation_subject($subject, $comment_id) {
    $site_name = get_bloginfo('name');
    $subject = "[$site_name] Comment ID $comment_id: Moderation Required";
    return $subject;
}
add_filter('comment_moderation_subject', 'my_custom_comment_moderation_subject', 10, 2);

Adding the author’s name to the subject

Include the comment author’s name in the email subject.

function add_author_name_to_subject($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $author_name = $comment->comment_author;
    $subject = "[$author_name] $subject";
    return $subject;
}
add_filter('comment_moderation_subject', 'add_author_name_to_subject', 10, 2);

Adding comment status to the subject

Include the comment status in the email subject.

function add_comment_status_to_subject($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $comment_status = $comment->comment_approved;
    $subject = "[$comment_status] $subject";
    return $subject;
}
add_filter('comment_moderation_subject', 'add_comment_status_to_subject', 10, 2);

Customizing the subject for specific post types

Change the subject for comments on a specific post type.

function custom_subject_for_post_type($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $post_id = $comment->comment_post_ID;
    $post_type = get_post_type($post_id);

    if ($post_type == 'product') {
        $subject = "Product Review: $subject";
    }

    return $subject;
}
add_filter('comment_moderation_subject', 'custom_subject_for_post_type', 10, 2);

Removing brackets from the subject

Remove brackets from the email subject.

function remove_brackets_from_subject($subject, $comment_id) {
    $subject = str_replace('[', '', $subject);
    $subject = str_replace(']', '', $subject);
    return $subject;
}
add_filter('comment_moderation_subject', 'remove_brackets_from_subject', 10, 2);