Using WordPress ‘comment_{$new_status}_{$comment->comment_type}’ PHP action

The comment_{$new_status}_{$comment->comment_type} WordPress PHP action fires when the status of a specific comment type is in transition.

Usage

add_action('comment_{$new_status}_{$comment->comment_type}', 'your_custom_function', 10, 2);

function your_custom_function($comment_id, $comment) {
    // your custom code here
}

Parameters

  • $comment_id: string – The comment ID as a numeric string.
  • $comment: WP_Comment – Comment object.

More information

See WordPress Developer Resources: comment_{$new_status}_{$comment->comment_type}

Examples

Notify author when their comment is approved

Send an email to the author when their comment is approved.

add_action('comment_approved_comment', 'notify_author_on_approval', 10, 2);

function notify_author_on_approval($comment_id, $comment) {
    $author_email = $comment->comment_author_email;
    $subject = 'Your comment has been approved';
    $message = 'Congratulations! Your comment has been approved on our website.';
    wp_mail($author_email, $subject, $message);
}

Delete spam trackbacks

Automatically delete spam trackbacks.

add_action('comment_spam_trackback', 'delete_spam_trackbacks', 10, 2);

function delete_spam_trackbacks($comment_id, $comment) {
    wp_delete_comment($comment_id, true);
}

Add custom action on comment unapproval

Perform a custom action when a comment is unapproved.

add_action('comment_unapproved_comment', 'custom_action_on_unapproval', 10, 2);

function custom_action_on_unapproval($comment_id, $comment) {
    // your custom code here
}

Log pingback approvals

Log pingback approvals to a custom log file.

add_action('comment_approved_pingback', 'log_pingback_approvals', 10, 2);

function log_pingback_approvals($comment_id, $comment) {
    $log_message = "Pingback with ID: {$comment_id} approved at " . current_time('mysql') . "\n";
    $log_file = fopen('pingback_approval_log.txt', 'a');
    fwrite($log_file, $log_message);
    fclose($log_file);
}

Send email notification on trackback spam

Send an email to the admin when a trackback is marked as spam.

add_action('comment_spam_trackback', 'notify_admin_on_trackback_spam', 10, 2);

function notify_admin_on_trackback_spam($comment_id, $comment) {
    $admin_email = get_option('admin_email');
    $subject = 'Trackback marked as spam';
    $message = "A trackback with ID: {$comment_id} has been marked as spam.";
    wp_mail($admin_email, $subject, $message);
}