Tell us what you are trying to do?
Hello support,
I want to generate automatic (something like autogenerate) 8 characters for the title of the custom post when clicking submit button. Not want to type in a field or something like that. Any help there?
Is there any documentation that you are following?
https://toolset.com/forums/topic/generate-a-unic-number-when-posting-a-new-post/
I need something like this, only to be generated on post titles and automatically on a slug.
Is there a similar example that we can see?
I not found on this forum
What is the link to your site?
It is on localhost
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
As I understand you want to generate the automatic post title for you post when you submit a new post using the Toolset form. If this is correct, you can use the Toolset form's API hook: cred_save_data
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
For example:
add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
if ($form_data['id']==9999) {
$randomid = mt_rand(10000000,99999999);
$slug = sanitize_title($randomid );
wp_update_post(array('ID'=>$post_id, 'post_title'=>$randomid ,'post_name' => $slug));
}
}
Where:
- Replace 9999 with your original form ID
My issue is resolved now. Thank you!
One more question, is it possible also that I can also generate on new title characters too, not only numbers?
Thank you.
One more thing. Is it possible that also I have characters there, not only numbers?
Thank you.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Yes - here is the code you should be using:
add_action('cred_save_data', 'func_build_auto_post_title', 10, 2);
function func_build_auto_post_title($post_id, $form_data) {
if ($form_data['id']==9999) {
$myRandomString = generateRandomString(8);
$slug = sanitize_title($myRandomString);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$myRandomString ,'post_name' => $slug));
}
}
function generateRandomString($length = 25) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
- - Replace 9999 with your original form ID
My issue is resolved now. Thank you one more time!