Adds a new comment to the database.
Filters new comment to ensure that the fields are sanitized and valid before inserting comment into database. Calls 'comment_post' action with comment ID and whether comment is approved by WordPress. Also has 'preprocess_comment' filter for processing the comment data before the function handles it.
<?php wp_new_comment( $commentdata ) ?>
global $post, $current_user; //for this example only :) $commentdata = array( 'comment_post_ID' => $post->ID, // to which post the comment will show up 'comment_author' => 'Another Someone', //fixed value - can be dynamic 'comment_author_email' => 'firstname.lastname@example.org', //fixed value - can be dynamic 'comment_author_url' => 'http://example.com', //fixed value - can be dynamic 'comment_content' => 'Comment messsage...', //fixed value - can be dynamic 'comment_type' => '', //empty for regular comments, 'pingback' for pingbacks, 'trackback' for trackbacks 'comment_parent' => 0, //0 if it's not a reply to another comment; if it's a reply, mention the parent comment ID here 'user_id' => $current_user->ID, //passing current user ID or any predefined as per the demand ); //Insert new comment and get the comment ID $comment_id = wp_new_comment( $commentdata );
wp_new_comment() is located in