wp_new_comment

SummerySummery

Adds a new comment to the database.

Syntax

wp_new_comment( array $commentdata, bool $avoid_die = false )

DescriptionDescription

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.

We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure that it is properly set, such as in wp-config.php, for your environment.

See https://core.trac.wordpress.org/ticket/9235

ParametersParameters

$commentdata

(array) (Required) Comment data.

  • 'comment_author'
    (string) The name of the comment author.
  • 'comment_author_email'
    (string) The comment author email address.
  • 'comment_author_url'
    (string) The comment author URL.
  • 'comment_content'
    (string) The content of the comment.
  • 'comment_date'
    (string) The date the comment was submitted. 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 GMT timezone.
  • 'comment_parent'
    (int) The ID of this comment's parent, if any. Default 0.
  • 'comment_post_ID'
    (int) The ID of the post that relates to the comment.
  • 'user_id'
    (int) The ID of the user who submitted the comment. Default 0.
  • 'user_ID'
    (int) Kept for backward-compatibility. Use $user_id instead.
  • 'comment_agent'
    (string) Comment author user agent. Default is the value of 'HTTP_USER_AGENT' in the $_SERVER superglobal sent in the original request.
  • 'comment_author_IP'
    (string) Comment author IP address in IPv4 format. Default is the value of 'REMOTE_ADDR' in the $_SERVER superglobal sent in the original request.

$avoid_die

(bool) (Optional) Should errors be returned as WP_Error objects instead of executing wp_die()?

Default value: false

ReturnReturn

(int|false|WP_Error) The ID of the comment on success, false or WP_Error on failure.

SourceSource

File: wp-includes/comment.php


			

ChangelogChangelog

Changelog
Version Description
4.7.0 The $avoid_die parameter was added, allowing the function to return a WP_Error object instead of dying.
4.3.0 'comment_agent' and 'comment_author_IP' can be set via $commentdata.
1.5.0 Introduced.

See alsoSee also

Scroll to Top