Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Automatically Insert Commentmeta when new comment WordPress

  • SOLVED

This php function below will add comment to wordpress database with comment id and everything that I need, here is the code.

$pfx = $wpdb->prefix;



$temps = get_option("commentatortemplates");

$rcount = rand(get_option("commentatorautoc"), get_option("commentatorautoc2"));

$names = get_option("commentatornames");

$pcount = 0;

if ($opc == 0)

{

$already = $wpdb->get_var("select count(comment_post_ID) from {$pfx}comments where comment_post_ID = $post_ID");

if ($already >= get_option("commentatorautoc"))

return 0;

}



$catop = "";

$temps = explode("\n", $temps);

$names = explode("\n", $names);

$domains = get_option("commentatormail");

$domains = explode("\n", $domains);

$nofmatchs = 0;



for ($i = 0; $i < $rcount; $i++)

{





$post = $post_ID;

if (1)

{



$prod = $temps[(rand(0, count($temps) - 1))];

$catss = wp_get_post_categories($post);



$zecat = get_the_category($post);

$zecat = $zecat[0]->cat_name;

$zetit = get_the_title($post);

$zename = trim($names[(rand(0, count($names) - 1))]);

$zename = str_replace(" ", ".", $zename);





$prod = str_replace("[cat]", $zecat, $prod);



$prod = str_replace("[title]", $zetit, $prod);



$idd = $post;



$tim = new DateTime(get_the_time('Y-m-d H:i:s', $post));





date_default_timezone_set(get_option('timezone_string'));

$nn = new DateTime(date_i18n('Y-m-d H:i:s'));



$diff = get_time_differencepro($tim->format('Y-m-d H:i:s'), $nn->format('Y-m-d H:i:s'));



if ($opc == 0)

{

$m = rand(1, 60 * $i / $rcount);

$h = rand(0, get_option("commentatorautoh") * $i / $rcount);

$l = ""; //you can modify this to put a site name

$tim->modify("+{$h} hours");

$tim->modify("+{$m} minutes");

}

if ($opc == 1)

{

$m = rand(1, $diff['minutes']);

$h = rand(0, $diff['hours']);



$dd = rand($diff['days'] * $i / $rcount, $diff['days'] * ($i + 1) / $rcount);





$tim->modify("+{$h} hours");



$tim->modify("+{$m} minutes");



$tim->modify("+{$dd} days");

}



$tim = $tim->format('Y-m-d H:i:s');



$domain = trim($domains[(rand(0, count($domains) - 1))]);

$authmail = $domain;
$zename = str_replace('.', ' ', $zename);





$wpdb->query("INSERT INTO `{$pfx}comments` (

`comment_ID` ,

`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`

)

VALUES (

NULL , '$idd', '$zename', '$authmail', '$l', '1.1.1.1', '$tim', '$tim', '$prod', '0', '1', 'X', '', '0', '0');");



}

}





wp_update_comment_count($post_ID);


So everytime I click on button it will automatically post new comment, that is working really great. But anyway when it add new comment, it will not add commentmeta that I will need. So I will need modify the code above that it will also add commentmeta.
for example.
meta_key - album
meta_value - ocean

Answers (1)

2014-10-02

timDesain Nanang answers:

You can try this code:
put on the end of line

$comment_id = $wpdb->insert_id;
add_comment_meta( $comment_id, 'album', 'ocean', true );


codex: http://codex.wordpress.org/Function_Reference/add_comment_meta


Chymmi comments:

LOL that works :D

wait I will test something and then I will let you know if this is 100% working


Chymmi comments:

I will need help a little bit modify this

$comment_id = $wpdb->insert_id;
$keyed = <<<shuffle>>>
add_comment_meta( $comment_id, 'album', $keyed, true );


It will be very good if $keyed will automatically shuffle or randomly choose every time new comment is posted
for example
1 comment - meta_key=ocean
2 comment - meta_key=sea
3 comment - meta_key=lake

thank you with this also!!


timDesain Nanang comments:

try this:

$values = array( 'ocean', 'sea', 'lake' );
$key = mt_rand(0, ((count($values)) - 1) );
add_comment_meta( $comment_id, 'album', $values[$key], true );


Chymmi comments:

Thank you it worked !