wp_delete_comment

Home » Functions » wp_delete_comment

Function Name: wp_delete_comment

The wp_delete_comment() function is a WordPress core function that is used to delete a comment from the database.

Comments are an integral part of most WordPress sites, and it’s important to be able to manage them effectively. wp_delete_comment() allows you to delete a comment by passing its ID as an argument.

This function not only deletes the comment from the comments table, but also deletes all related metadata, such as comment meta, and also removes the comment from any associated comment threads.

Here’s an example usage code for wp_delete_comment():

$comment_id = 123; // Replace 123 with the ID of the comment you want to delete
$deleted = wp_delete_comment( $comment_id, true ); // This will delete the comment permanently

if ( $deleted ) {
    echo "Comment deleted successfully!";
} else {
    echo "There was an error deleting the comment.";
}

In this example, we first set the $comment_id variable to the ID of the comment we want to delete. We then call wp_delete_comment() with the $comment_id argument, as well as a second argument of true, which tells WordPress to permanently delete the comment and all related data.

We then check the return value of wp_delete_comment() and output a success message if the comment was deleted successfully, or an error message if there was a problem.

Learn More on WordPress.org

WordPress snippets using the wp_delete_comment function

Register an account to save your snippets or go Pro to get more features.