wp_delete_comment()
云策文档标注
概述
wp_delete_comment() 函数用于将评论移至回收站或永久删除,具体行为取决于参数设置和系统配置。它会更新相关数据,如评论计数,并触发多个钩子以支持扩展功能。
关键要点
- 默认情况下,评论被移至回收站而非永久删除,除非回收站被禁用、评论已在回收站或 $force_delete 参数为 true。
- 函数接受两个参数:$comment_id(必需,评论ID或WP_Comment对象)和 $force_delete(可选,强制删除标志,默认为false)。
- 返回布尔值:成功时返回 true,失败时返回 false。
- 在删除过程中,会处理子评论的层级关系、删除评论元数据,并更新相关文章的评论计数(如果评论已批准)。
- 触发多个钩子,如 delete_comment、deleted_comment 和 wp_set_comment_status,允许开发者介入删除流程。
代码示例
function wp_delete_comment( $comment_id, $force_delete = false ) {
global $wpdb;
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ), true ) ) {
return wp_trash_comment( $comment_id );
}
do_action( 'delete_comment', $comment->comment_ID, $comment );
// 处理子评论和元数据的代码省略...
return true;
}注意事项
- 使用 $force_delete 参数时需谨慎,因为它会绕过回收站直接永久删除评论。
- 确保在调用前检查评论是否存在,以避免不必要的错误。
- 钩子如 delete_comment 和 deleted_comment 可用于在删除前后执行自定义操作。
原文内容
Trashes or deletes a comment.
Description
The comment is moved to Trash instead of permanently deleted unless Trash is disabled, item is already in the Trash, or $force_delete is true.
The post comment count will be updated if the comment was approved and has a post ID available.
Parameters
$comment_idint|WP_Commentrequired-
Comment ID or WP_Comment object.
$force_deletebooloptional-
Whether to bypass Trash and force deletion.
Default:
false
Source
function wp_delete_comment( $comment_id, $force_delete = false ) {
global $wpdb;
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ), true ) ) {
return wp_trash_comment( $comment_id );
}
/**
* Fires immediately before a comment is deleted from the database.
*
* @since 1.2.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment to be deleted.
*/
do_action( 'delete_comment', $comment->comment_ID, $comment );
// Move children up a level.
$children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );
if ( ! empty( $children ) ) {
$wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );
clean_comment_cache( $children );
}
// Delete metadata.
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
foreach ( $meta_ids as $mid ) {
delete_metadata_by_mid( 'comment', $mid );
}
if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) {
return false;
}
/**
* Fires immediately after a comment is deleted from the database.
*
* @since 2.9.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The deleted comment.
*/
do_action( 'deleted_comment', $comment->comment_ID, $comment );
$post_id = $comment->comment_post_ID;
if ( $post_id && '1' === $comment->comment_approved ) {
wp_update_comment_count( $post_id );
}
clean_comment_cache( $comment->comment_ID );
/** This action is documented in wp-includes/comment.php */
do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );
wp_transition_comment_status( 'delete', $comment->comment_approved, $comment );
return true;
}
Hooks
- do_action( ‘deleted_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately after a comment is deleted from the database.
- do_action( ‘delete_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately before a comment is deleted from the database.
- do_action( ‘wp_set_comment_status’, string $comment_id, string $comment_status )
-
Fires immediately after transitioning a comment’s status from one to another in the database and removing the comment from the object cache, but prior to all status transition hooks.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |