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

auto insert date and time (string) into comment_content WordPress

  • SOLVED

This is an add-on request to this previous (solved) question...
https://wpquestions.com/I_want_to_be_able_to_submit_a_comment_with_a_random_20_digit_number_auto_inserted/27781
The previous question added another 20 digit number every time the comment was updated. I don't want that to happen... only when the comment is first posted.

I now want to insert a date and time string AND a random 20 digit number into the comment text... e.g.

16th March 2020 at 2030hrs (and the 20 digit number) will look like...
2020-03-16-2030 - 12345678909876512345

Answers (1)

2020-05-17

Rempty answers:

Hello
try this code

function filter_preprocess_comment_rem( $commentdata ) {
$content=$commentdata['comment_content'];
$date=date("Y-m-d H:i");
$random=bwd_random_code2();
$content=$content." ".$date." - ".$random;
$commentdata['comment_content']=$content;
return $commentdata;
};

add_filter( 'preprocess_comment', 'filter_preprocess_comment_rem', 10 );

function bwd_random_code2($digits = 20) {
$code = [];
for ($i = 0; $i < 20; $i++) {
$code[] = rand(0, 9);
}

return implode("", $code);
}


pjeaje comments:

Thanks