删除或删除评论。
原型
wp_delete_comment( int|WP_Comment $comment_id, bool $force_delete = false )
描述
注释将移至垃圾箱而不是永久删除,除非已禁用垃圾箱,商品已在垃圾箱中,或者$ force_delete为true。
参数
$comment_id
(int|WP_Comment)
(Required)
注释ID或WP_Comment对象。
$force_delete
(bool)
(Optional)
是否绕过垃圾并强行删除。默认值为false。
返回值
(bool)
成功时是真的,失败时是假的。
源文件
路径:wp-includes/comment.php
<?php
...
function wp_delete_comment($comment_id, $force_delete = false) {
global $wpdb;
if (!$comment = get_comment($comment_id))
return false;
if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) )
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 int $comment_id The comment ID.
* @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 int $comment_id The comment ID.
* @param WP_Comment $comment The deleted comment.
*/
do_action( 'deleted_comment', $comment->comment_ID, $comment );
$post_id = $comment->comment_post_ID;
if ( $post_id && $comment->comment_approved == 1 )
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;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_delete_comment/