Skip Navigation

[Resolved] Want generate 8 characters on post titles of custom post

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by darioM-3 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2109191
Screenshot 2021-07-08 at 13.32.02.png
Screenshot 2021-07-08 at 13.32.10.png

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

#2109213

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

#2109221

My issue is resolved now. Thank you!

#2109223
Screenshot 2021-07-08 at 14.12.18.png

One more question, is it possible also that I can also generate on new title characters too, not only numbers?

Thank you.

#2109237
Screenshot 2021-07-08 at 14.12.18.png

One more thing. Is it possible that also I have characters there, not only numbers?

Thank you.

#2109247

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

#2109249

My issue is resolved now. Thank you one more time!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.