Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp new comment


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$avoid_die false ?>


(array) (required) Contains information on the comment.
Default: None
(boolean) (required) Should errors be returned as WP_Error objects instead of executing wp_die()? Default false.
Default: None

Return Values

The ID of the comment after adding.


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' => 'someone@example.com', //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 );


Change Log

Since: 1.5.0

Source File

wp_new_comment() is located in wp-includes/comment.php.


See also index of Function Reference and index of Template Tags.