wp_insert_comment

Inserts a comment into the database.

Description

(int|false) wp_insert_comment( (array) $commentdata ); 

Returns (int|false)

The new comment's ID on success, false on failure.

Parameters (1)

0. $commentdata (array)
Array of arguments for inserting a new comment.

Options

  • comment_agent (string) => ''

    The HTTP user agent of the $comment_author when the comment was submitted.

  • comment_approved (int|string) => 1

    Whether the comment has been approved.

  • comment_author (string) => ''

    The name of the author of the comment.

  • comment_author_email (string) => ''

    The email address of the $comment_author..

  • comment_author_IP (string) => ''

    The IP address of the $comment_author..

  • comment_author_url (string) => ''

    The URL address of the $comment_author..

  • comment_content (string) => ''

    The content of the comment.

  • comment_date (string) => is the current time

    The date the comment was submitted. To set the date manually, $comment_date_gmt must also be specified.

  • comment_date_gmt (string) => 'is $comment_date in the site'

    The date the comment was submitted in the GMT timezone.

  • comment_karma (int) => 0

    The karma of the comment.

  • comment_parent (int) => 0

    ID of this comment's parent, if any.

  • comment_post_ID (int) => 0

    ID of the post that relates to the comment, if any.

  • comment_type (string) => ''

    Comment type.

  • comment_meta (array) => null

    Array of key/value pairs to be stored in commentmeta for the new comment.

array(

    /**
     * The HTTP user agent of the `$comment_author` when the comment was submitted.
     *
     * @type string
     * @default ''
     */
    'comment_agent' => '',

    /**
     * Whether the comment has been approved.
     *
     * @type int|string
     * @default 1
     */
    'comment_approved' => 1,

    /**
     * The name of the author of the comment.
     *
     * @type string
     * @default ''
     */
    'comment_author' => '',

    /**
     * The email address of the `$comment_author`.
     *
     * @type string
     * @default ''
     */
    'comment_author_email' => '',

    /**
     * The IP address of the `$comment_author`.
     *
     * @type string
     * @default ''
     */
    'comment_author_IP' => '',

    /**
     * The URL address of the `$comment_author`.
     *
     * @type string
     * @default ''
     */
    'comment_author_url' => '',

    /**
     * The content of the comment.
     *
     * @type string
     * @default ''
     */
    'comment_content' => '',

    /**
     * The date the comment was submitted. To set the date manually, `$comment_date_gmt` must also
     * be specified.
     *
     * @type string
     * @default is the current time
     */
    'comment_date' => is the current time,

    /**
     * The date the comment was submitted in the GMT timezone.
     *
     * @type string
     * @default 'is $comment_date in the site'
     */
    'comment_date_gmt' => 'is $comment_date in the site',

    /**
     * The karma of the comment.
     *
     * @type int
     */
    'comment_karma' => 0,

    /**
     * ID of this comment's parent, if any.
     *
     * @type int
     */
    'comment_parent' => 0,

    /**
     * ID of the post that relates to the comment, if any.
     *
     * @type int
     */
    'comment_post_ID' => 0,

    /**
     * Comment type.
     *
     * @type string
     * @default ''
     */
    'comment_type' => '',

    /**
     * Optional. Array of key/value pairs to be stored in commentmeta for the new comment.
     *
     * @type array
     * @default null
     * @optional
     */
    'comment_meta' => null
);        


Usage

  1. if ( !function_exists( 'wp_insert_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Array of arguments for inserting a new comment. 
  5. $commentdata = array( 
  6. 'comment_agent' => '', 
  7. 'comment_approved' => 1, 
  8. 'comment_author' => '', 
  9. 'comment_author_email' => '', 
  10. 'comment_author_IP' => '', 
  11. 'comment_author_url' => '', 
  12. 'comment_content' => '', 
  13. 'comment_date' => is the current time, 
  14. 'comment_date_gmt' => 'is $comment_date in the site', 
  15. 'comment_karma' => 0, 
  16. 'comment_parent' => 0, 
  17. 'comment_post_ID' => 0, 
  18. 'comment_type' => '', 
  19. 'comment_meta' => null 
  20. ); 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = wp_insert_comment($commentdata); 
  24.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function wp_insert_comment( $commentdata ) { 
  2. global $wpdb; 
  3. $data = wp_unslash( $commentdata ); 
  4.  
  5. $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author']; 
  6. $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email']; 
  7. $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url']; 
  8. $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP']; 
  9.  
  10. $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date']; 
  11. $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt']; 
  12.  
  13. $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID']; 
  14. $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content']; 
  15. $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; 
  16. $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; 
  17. $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; 
  18. $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type']; 
  19. $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; 
  20.  
  21. $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; 
  22.  
  23. $compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' ); 
  24. if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) { 
  25. return false; 
  26.  
  27. $id = (int) $wpdb->insert_id; 
  28.  
  29. if ( $comment_approved == 1 ) { 
  30. wp_update_comment_count( $comment_post_ID ); 
  31.  
  32. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 
  33. wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); 
  34.  
  35.  
  36. $comment = get_comment( $id ); 
  37.  
  38. // If metadata is provided, store it. 
  39. if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) { 
  40. foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) { 
  41. add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true ); 
  42.  
  43. /** 
  44. * Fires immediately after a comment is inserted into the database. 
  45. * @since 2.8.0 
  46. * @param int $id The comment ID. 
  47. * @param WP_Comment $comment Comment object. 
  48. */ 
  49. do_action( 'wp_insert_comment', $id, $comment ); 
  50.  
  51. return $id;