Post Pic

WordPress hack: Insert comments programatically

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' => '127.0.0.1',
	'comment_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) 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);

4 Responses

Aug 01 2012 10:10

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.

Feb 20 2013 09:23

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

Mar 25 2013 08:03

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

Apr 29 2013 21:18

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

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox