wp_insert_comment

Advertisement

Summery Summery

Inserts a comment into the database.

Syntax Syntax

wp_insert_comment( array $commentdata )

Parameters Parameters

$commentdata

(Required) Array of arguments for inserting a new comment.

  • 'comment_agent'
    (string) The HTTP user agent of the $comment_author when the comment was submitted. Default empty.
  • 'comment_approved'
    (int|string) Whether the comment has been approved. Default 1.
  • 'comment_author'
    (string) The name of the author of the comment. Default empty.
  • 'comment_author_email'
    (string) The email address of the $comment_author. Default empty.
  • 'comment_author_IP'
    (string) The IP address of the $comment_author. Default empty.
  • 'comment_author_url'
    (string) The URL address of the $comment_author. Default empty.
  • 'comment_content'
    (string) The content of the comment. Default empty.
  • 'comment_date'
    (string) The date the comment was submitted. To set the date manually, $comment_date_gmt must also be specified. Default is the current time.
  • 'comment_date_gmt'
    (string) The date the comment was submitted in the GMT timezone. Default is $comment_date in the site's GMT timezone.
  • 'comment_karma'
    (int) The karma of the comment. Default 0.
  • 'comment_parent'
    (int) ID of this comment's parent, if any. Default 0.
  • 'comment_post_ID'
    (int) ID of the post that relates to the comment, if any. Default 0.
  • 'comment_type'
    (string) Comment type. Default empty.
  • 'comment_meta'
    (array) Optional. Array of key/value pairs to be stored in commentmeta for the new comment.
  • 'user_id'
    (int) ID of the user who submitted the comment. Default 0.

Return Return

(int|false) The new comment's ID on success, false on failure.

Source Source

File: wp-includes/comment.php

	$comment_author = '';
	if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
		$comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ];
	}

	$comment_author_email = '';
	if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
		$comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ];
	}

	$comment_author_url = '';
	if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
		$comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ];
	}

	/**
	 * Filters the current commenter's name, email, and URL.
	 *
	 * @since 3.1.0
	 *
	 * @param array $comment_author_data {
	 *     An array of current commenter variables.
	 *
	 *     @type string $comment_author       The name of the current commenter, or an empty string.
	 *     @type string $comment_author_email The email address of the current commenter, or an empty string.
	 *     @type string $comment_author_url   The URL address of the current commenter, or an empty string.
	 * }
	 */
	return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
}

/**
 * Get unapproved comment author's email.
 *
 * Used to allow the commenter to see their pending comment.
 *
 * @since 5.1.0
 *
 * @return string The unapproved comment author's email (when supplied).
 */
function wp_get_unapproved_comment_author_email() {
	$commenter_email = '';

	if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
		$comment_id = (int) $_GET['unapproved'];
		$comment    = get_comment( $comment_id );

		if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
			// The comment will only be viewable by the comment author for 1 minute.
			$comment_preview_expires = strtotime( $comment->comment_date_gmt . '+1 minute' );

			if ( time() < $comment_preview_expires ) {
				$commenter_email = $comment->comment_author_email;
			}
		}
	}

	if ( ! $commenter_email ) {
		$commenter       = wp_get_current_commenter();

Advertisement

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced $comment_meta argument.
2.0.0 Introduced.

Advertisement

Leave a Reply