DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Some time ago, I’ve shown you how to insert posts programatically in WordPress database. So now, what about comments? In this recipe I’ll show you

The following code can be pasted anywhere on your theme files.
Once this code is executed, it will add a new comment into WordPress database. The returned value is the comment ID, or 0 if a problem happenned.

$data = array(
	'comment_post_ID' => 1,
	'comment_author' => 'admin',
	'comment_author_email' => 'admin@admin.com',
	'comment_author_url' => 'http://www.catswhocode.com',
	'comment_content' => 'Lorem ipsum dolor sit amet...',
	'comment_author_IP' => '',
	'comment_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv: Gecko/20100401 Firefox/3.6.3',
	'comment_date' => date('Y-m-d H:i:s'),
	'comment_date_gmt' => date('Y-m-d H:i:s'),
	'comment_approved' => 1,

$comment_id = wp_insert_comment($data);


  1. This a great wordpress hack. I didn’t know about it. I just got this site on search engine. And I must say, it has some good articles about wordpress.

  2. Great hack, it saved me from converting my form to WP comments form. I can just submit my form as a comment now!

  3. nice one. cant find information on manually inserting data to WP using clean sql =(

  4. Good Post. I would like to know how to insert replies to comments programatically?

Leave a Reply to Nikita Mishra Cancel

Your email address will not be published. Required fields are marked *