钩子文档

wp_insert_comment

💡 云策文档标注

概述

wp_insert_comment 是一个 WordPress Hook,在评论被插入数据库后立即触发。它允许开发者在评论插入时执行自定义操作,例如处理回复评论。

关键要点

  • Hook 名称:wp_insert_comment
  • 触发时机:评论插入数据库后立即执行
  • 参数:$id(评论 ID,整数类型)和 $comment(WP_Comment 对象)
  • 相关函数:wp_insert_comment() 用于插入评论
  • 引入版本:WordPress 2.8.0

代码示例

add_action( 'wp_insert_comment', 'wporg_comment_inserted', 99, 2);

function wporg_comment_inserted( $comment_id, $comment_object ) {
    if ( $comment_object->comment_parent > 0 ) {
        // do something
    }
}

注意事项

  • 此 Hook 在评论插入后触发,可用于基于评论数据(如父评论 ID)执行后续逻辑。
  • 参数顺序和类型需与示例一致,以确保正确接收数据。

📄 原文内容

Fires immediately after a comment is inserted into the database.

Parameters

$idint
The comment ID.
$commentWP_Comment
Comment object.

Source

do_action( 'wp_insert_comment', $id, $comment );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes