delete comment meta when comment is delete from database

Posted: July 16, 2015 in Uncategorized
Tags: , , ,

Recently I just added image with comment(user can set image when he/she entered his/her comment)..My strategy is when comment is being saved just add image id with comment meta after get attachment_id..:)
But problem is when admin delete comment ..comment meta is not deleted..
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’ );
} );

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s