Advertisements

wp_new_comment

Definition:
function wp_new_comment( $commentdata ) {}

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.

Parameters

  • array $commentdata: Contains information on the comment.

Return values

returns:The ID of the comment after adding.

Defined filters

  • preprocess_comment
    apply_filters('preprocess_comment', $commentdata)

Defined actions

  • comment_post
    do_action('comment_post', $comment_ID, $commentdata['comment_approved']);

Source code

function wp_new_comment( $commentdata ) {

	$commentdata = apply_filters('preprocess_comment', $commentdata);



	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];

	if ( isset($commentdata['user_ID']) )

		$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];

	elseif ( isset($commentdata['user_id']) )

		$commentdata['user_id'] = (int) $commentdata['user_id'];



	$commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;

	$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';

	$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;



	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );

	$commentdata['comment_agent']     = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);



	$commentdata['comment_date']     = current_time('mysql');

	$commentdata['comment_date_gmt'] = current_time('mysql', 1);



	$commentdata = wp_filter_comment($commentdata);



	$commentdata['comment_approved'] = wp_allow_comment($commentdata);



	$comment_ID = wp_insert_comment($commentdata);



	do_action('comment_post', $comment_ID, $commentdata['comment_approved']);



	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching

		if ( '0' == $commentdata['comment_approved'] )

			wp_notify_moderator($comment_ID);



		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment



		if ( get_option('comments_notify') && $commentdata['comment_approved'] && ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) )

			wp_notify_postauthor($comment_ID, isset( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '' );

	}



	return $comment_ID;

}

3941

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: