Quantcast
Channel: A HitchHackers guide through WordPress » Filters by letter p
Viewing all articles
Browse latest Browse all 10

wp_new_comment

$
0
0

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


Filed under: /wp-includes/comment.php, Actions, Actions by letter c, Documentation, Files, Filters, Filters by letter p, Functions, Functions by letter w Tagged: codex, comment_post, idc0e03d18efa5bd42103b7ee65a99bd44, preprocess_comment, wp_new_comment

Viewing all articles
Browse latest Browse all 10

Trending Articles