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);

7 Responses

May 17 2010 12:08

Is there a way of getting the ‘comment_agent’ using php. I guess getting the IP can be done using ‘$_SERVER['REMOTE_ADDR']‘ but I don’t know how to get the agent using php.

May 17 2010 12:24

@TahaH: $_SERVER['HTTP_USER_AGENT'] detext the user agent :)

May 17 2010 14:41

@Jean: Do you how can I get the post count in a certain category in wordpress. Say I have a category called ‘movies’, I want to it to get the post count in movies like ’4 posts in movies’ if you get me.

Any suggestions

May 17 2010 14:56

Edit: Post count by a certain author in a certain category

May 17 2010 14:57

My company can do that for you for a fair price. Email me if you’re interested.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required