wp_spam_comment()
云策文档标注
概述
wp_spam_comment() 函数用于将评论标记为垃圾评论,接受评论 ID 或 WP_Comment 对象作为参数,返回操作成功与否的布尔值。
关键要点
- 参数:$comment_id,类型为 int 或 WP_Comment 对象,必需,指定要标记为垃圾的评论。
- 返回值:bool 类型,成功返回 true,失败返回 false。
- 内部流程:通过 wp_set_comment_status() 设置评论状态为 'spam',并更新相关元数据。
- 钩子:提供 spam_comment 和 spammed_comment 两个动作钩子,分别在标记前后触发。
- 相关函数:包括 wp_set_comment_status()、delete_comment_meta()、add_comment_meta() 等。
代码示例
function wp_spam_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
do_action( 'spam_comment', $comment->comment_ID, $comment );
if ( wp_set_comment_status( $comment, 'spam' ) ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
do_action( 'spammed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}注意事项
- 函数在 WordPress 2.9.0 版本引入,后续版本可能更新钩子参数。
- 使用前需确保评论存在,否则返回 false。
- 钩子 spam_comment 和 spammed_comment 可用于自定义操作,如日志记录或通知。
原文内容
Marks a comment as Spam.
Parameters
$comment_idint|WP_Commentrequired-
Comment ID or WP_Comment object.
Source
function wp_spam_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
/**
* Fires immediately before a comment is marked as Spam.
*
* @since 2.9.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param int $comment_id The comment ID.
* @param WP_Comment $comment The comment to be marked as spam.
*/
do_action( 'spam_comment', $comment->comment_ID, $comment );
if ( wp_set_comment_status( $comment, 'spam' ) ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
/**
* Fires immediately after a comment is marked as Spam.
*
* @since 2.9.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param int $comment_id The comment ID.
* @param WP_Comment $comment The comment marked as spam.
*/
do_action( 'spammed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}
Hooks
- do_action( ‘spammed_comment’, int $comment_id, WP_Comment $comment )
-
Fires immediately after a comment is marked as Spam.
- do_action( ‘spam_comment’, int $comment_id, WP_Comment $comment )
-
Fires immediately before a comment is marked as Spam.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |