1

I add some input fields to the comment form, and than this new data is stored on commentmeta table. When a comment is deleted, the data on commentmeta is still there.

How can I delete these data when a comment is deleted? (I need an action similar to the delete_post but for comments...does it exists?)

4

Look at the function wp_delete_comment(). It fires an action before the comment is deleted:

/**
 * Fires immediately before a comment is deleted from the database.
 *
 * @since 1.2.0
 *
 * @param int $comment_id The comment ID.
 */
do_action( 'delete_comment', $comment_id );

… and one after deletion:

/**
 * Fires immediately after a comment is deleted from the database.
 *
 * @since 2.9.0
 *
 * @param int $comment_id The comment ID.
 */
do_action( 'deleted_comment', $comment_id );

So you can bind your callback to that:

add_action( 'deleted_comment', function( $comment_id ) {
    delete_comment_meta( $comment_id, 'your_meta_key' );
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.