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

I want to be able to submit a comment with a random 20 digit number auto inserted. WordPress

  • SOLVED

I want to be able to submit a comment with a random 20 digit number auto inserted.

Answers (2)

2020-05-17

Andrea P answers:

The following code will append something like the string below to each submitted comment:

Code: 83677808926247654561

add_filter('pre_comment_content', 'bwd_add_code_to_comment');
function bwd_add_code_to_comment($comment_content) {

$add_this = ' Code: ' . bwd_random_code();
$comment_content = $comment_content . $add_this;

return $comment_content;
}

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

return implode("", $code);
}


pjeaje comments:

Thanks


Andrea P comments:

Don't forget to select the correct answer. ;)
thanks!

2020-05-17

Bob answers:

means when user submit event this auto generated number must be added automatically to comment?

can you clarify step by step how it should work?

should number be attached to that comment?


Bob comments:

if you want to just attach 20 digit id with comment then try wp_insert_comment action hook and attach number as custom comment meta with the help of update_comment_meta

https://developer.wordpress.org/reference/functions/wp_insert_comment/
https://developer.wordpress.org/reference/functions/update_comment_meta/

if you want to add 20 digit number id at the end of beginning of comment you can try wp_update_comment

https://developer.wordpress.org/reference/functions/wp_update_comment/

let me know how exactly it should work?


pjeaje comments:

When someone posts a comment (the textbox will be hidden) I'd like their comment to be auto inserted with some random numbers so I don't get an error message.


pjeaje comments:

I don't know how to use those functions


pjeaje comments:

When someone posts a comment (the textbox will be hidden) I'd like their comment to be auto inserted with some random numbers so THEY don't get an error message.