wp_insert_comment
is both a hook and a function. Once a comment is left on your blog, this action hook will be triggered, allowing you to modify the comment.
A very simple example would be:
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
}
If you attempt and add a comment by using wp_insert_comment
, then this function will return the ID of the inserted comment after it’s done:
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;
}
Now, if you call the add_new_comment()
like the following, it will add a new comment and return its ID:
echo add_new_comment('My First Commnent', '123' , 'Admin' , '1');
Which is what you are looking for.