wp_trash_comment()
Moves a comment to the Trash.
Stores the comment's previous status and the time it was trashed. This data is used when restoring it with wp_untrash_comment().
If EMPTY_TRASH_DAYS = 0, control is passed to wp_delete_comment().
When processing a top-level comment of the note type, the function also trashes or deletes its direct child comments of the note type.
The function does not check user capabilities or the nonce. When called from a user request, perform these checks separately.
Processing child notes is not a transaction. The function can return false when the parent note has already been trashed but processing one of the child notes failed.
Use wp_untrash_comment() to restore a comment.
Use wp_delete_comment() to delete a comment.
Hooks from the function
Returns
bool.
true- the comment and related child notes were successfully trashed or deleted.false- the comment was not found, its status could not be changed or it could not be deleted, or one of the child notes could not be processed.
Usage
wp_trash_comment( $comment_id );
- $comment_id(int|WP_Comment) (required)
- Comment ID or comment object.
Examples
#1 Move a comment to the Trash
Check the current user's capabilities and move the comment to the Trash:
$comment_id = 123;
if ( current_user_can( 'edit_comment', $comment_id ) ) {
$result = wp_trash_comment( $comment_id );
if ( ! $result ) {
wp_die( 'Could not move the comment to the Trash.' );
}
}Changelog
| Since 2.9.0 | Introduced. |
| Since 6.9.0 | Any child notes are deleted when deleting a note. |