Using WordPress ‘edit_comment’ PHP action

The edit_comment WordPress PHP action fires immediately after a comment is updated in the database. It also triggers before comment status transition hooks are fired.

Usage

add_action('edit_comment', 'your_function_name', 10, 2);

function your_function_name($comment_id, $data) {
    // your custom code here
}

Parameters

  • $comment_id (int) – The comment ID.
  • $data (array) – Comment data.

More information

See WordPress Developer Resources: edit_comment

Examples

Log comment edit

Log when a comment has been edited.

add_action('edit_comment', 'log_comment_edit', 10, 2);

function log_comment_edit($comment_id, $data) {
    error_log("Comment {$comment_id} has been edited.");
}

Update comment metadata

Update custom metadata when a comment is edited.

add_action('edit_comment', 'update_comment_metadata', 10, 2);

function update_comment_metadata($comment_id, $data) {
    update_comment_meta($comment_id, 'custom_key', 'custom_value');
}

Send email notification

Send an email notification to the admin when a comment is edited.

add_action('edit_comment', 'send_email_notification', 10, 2);

function send_email_notification($comment_id, $data) {
    wp_mail(get_option('admin_email'), 'A comment has been edited', "Comment {$comment_id} has been edited.");
}

Update comment rating

Update a custom comment rating based on the comment’s content.

add_action('edit_comment', 'update_comment_rating', 10, 2);

function update_comment_rating($comment_id, $data) {
    $rating = calculate_rating_based_on_content($data['comment_content']);
    update_comment_meta($comment_id, 'comment_rating', $rating);
}

Moderate comment length

Automatically hold comments longer than a specified length for moderation.

add_action('edit_comment', 'moderate_comment_length', 10, 2);

function moderate_comment_length($comment_id, $data) {
    if (strlen($data['comment_content']) > 1000) {
        wp_set_comment_status($comment_id, 'hold');
    }
}