wp_insert_comment
都是hook 和afunction. 在博客上留下评论后,将触发此操作挂钩,允许您修改评论。
一个非常简单的例子是:
add_action(\'wp_insert_comment\',\'my_function\',100,2);
function my_function($comment_id, $comment_object) {
// Now you have access to $comment_id, save it, print it, do whatever you want with it
}
如果您尝试使用
wp_insert_comment
, 完成后,此函数将返回插入注释的ID:
function add_new_comment($input, $post_id, $author, $user_id){
// Get the current time
$time = current_time(\'mysql\');
// Set the arguments
$data = array(
\'comment_post_ID\' => $post_id,
\'comment_author\' => $author,
\'comment_author_email\' => \'[email protected]\',
\'comment_author_url\' => \'URL HERE\',
\'comment_content\' => $input,
\'comment_type\' => \'\',
\'comment_parent\' => 0,
\'user_id\' => $user_id,
\'comment_author_IP\' => \'IP HERE\',
\'comment_agent\' => \'USER AGENT HERE\',
\'comment_date\' => $time,
\'comment_approved\' => 1,
);
// Store the ID in a variable
$id = wp_insert_comment($data);
// Return the ID
return $id;
}
现在,如果你打电话给
add_new_comment()
与以下内容类似,它将添加新注释并返回其ID:
echo add_new_comment(\'My First Commnent\', \'123\' , \'Admin\' , \'1\');
这就是你要找的。