WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp insert comment

Description

Inserts a comment to the database.

The available $commentdata key names are 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.

Also, consider using wp_new_comment(), which sanitizes and validates comment data before calling wp_insert_comment() to insert the comment into the database.

Usage

<?php

$time = current_time('mysql');

$data = array(
    'comment_post_ID' => 1,
    'comment_author' => 'admin',
    'comment_author_email' => 'admin@admin.com',
    'comment_author_url' => 'http://',
    'comment_content' => 'content here',
    'comment_type' => '',
    'comment_parent' => 0,
    'user_id' => 1,
    'comment_author_IP' => '127.0.0.1',
    'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
    'comment_date' => $time,
    'comment_approved' => 1,
);

wp_insert_comment($data);

?>

Parameters

$commentdata
(array) (required) Contains information on the comment.
Default: None

Return Values

(integer) 
The new comment's ID.
(boolean) 
`false` if the insert failed.

Examples

Notes

Change Log

Since: 2.0.0

Source File

wp_insert_comment() is located in wp-includes/comment.php.

Related

See also index of Function Reference and index of Template Tags.