I have the following code to save a comment from a custom form, via POST:
if (!empty($_POST)) {
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $_POST['comment_post_id'],
'comment_author' => $_POST['author'],
'comment_author_email' => $_POST['email'],
'comment_content' => $_POST['comment_content'],
'comment_type' => '',
'comment_parent' => $_POST['comment_parent'],
'comment_author_IP' => $_POST['author_IP'],
'comment_date' => $time,
'comment_approved' => 0
);
wp_insert_comment($data);
}
Everything is working fine, but how do I add a custom field to $data
? My custom field is already created, and I’m using:
update_comment_meta( $_POST['comment_post_id'], 'author_avatar', $_POST['author_avatar'] );
To register the author avatar on the custom field. But when the comment is saved, the field appears to be empty.
Does wp_insert_comment
accept custom fields? Any suggestions?
You can add an item called ‘comment_meta’ to the $data and pass an array to that. Check this documentation to see what you are allowed to pass to wp_insert_data.
https://developer.wordpress.org/reference/functions/wp_insert_comment/