Skip Navigation

[Resolved] Generate a random number and letters in custom field

This thread is resolved. Here is a description of the problem and solution.

Problem:

Use cred_save_data action hook to update a custom field value.

Solution:

You can try these:

1) Create a post form for editing post, so you will can setup submit button in the form

2) Create another custom codes, use update_post_meta() to update the custom field value, for example:

https://toolset.com/forums/topic/generate-a-random-number-and-letters-in-custom-field/#post-1856061

Relevant Documentation:

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

https://developer.wordpress.org/reference/functions/wp_update_post/#user-contributed-notes

This support ticket is created 4 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by avansisI-2 4 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1855671

Good afternoon,

I'm trying to create a random number that is automatically generated in a custom type when I hit a button.

I was looking at other related information,
https://toolset.com/forums/topic/generate-a-unic-number-when-posting-a-new-post/

and I created this code. But how do I link it to a button? And how i can use this number like a custom type title?

//NÚMERO ALEATORIO BONO
//
add_action('cred_save_data', 'id_for_posts_rand_seven_digit',10,2);
function id_for_posts_rand_seven_digit($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==5908)
    {
            $seven_digit_random_number = mt_rand(1000000,99999999);
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-referencia-del-bono', $seven_digit_random_number, true);
    }
}
#1856061

Hello,

There isn't such kind of built-in feature within Toolset plugins.

And the custom codes you mentioned above is for creating a new post.

I assume you are going to update existed posts, if it is, you can try these:
1) Create a post form for editing post, so you will can setup submit button in the form
2) Create another custom codes, use update_post_meta() to update the custom field value, for example:

add_action('cred_save_data', 'update_id_for_posts_rand_seven_digit',10,2);
function update_id_for_posts_rand_seven_digit($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==123) // replace 123 with your edit post form ID
    {
            $seven_digit_random_number = mt_rand(1000000,99999999);
            // add it to saved post meta
            update_post_meta($post_id, 'wpcf-referencia-del-bono', $seven_digit_random_number);
    }
}

3) If you want to update the post title too, please try WP function wp_update_post(), see WP document:
https://developer.wordpress.org/reference/functions/wp_update_post/
and example in above document:
https://developer.wordpress.org/reference/functions/wp_update_post/#user-contributed-notes

#1856419

Thanks Luo.

I think I explained wrong

My idea is to put generate button and that action creates a code with a random number and with the date automatically that I have already done.

that number has to be the title of the custom type created and it must be unique

Could you help me?
thanks

#1856421

Thanks for the details, the random number won't be unique, within WordPress, you can use post ID as unique number, you can use Views shortcode [wpv-post-id] to display it in front-end, see our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-id

#1856523

Hi Luo,

my code to add is this

//REFERENCIA AUTOMÁTICA
add_action('cred_save_data', 'id_for_posts_using_post_id',10,2);
function id_for_posts_using_post_id($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==5908)
    {
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-referencia-del-bono', $post_id, true);
    }
}

But i dont know why is wrong. Could you help me?

#1856529

otherwise this code is accepted but nothing happens.

//REFERENCIA AUTOMÁTICA
add_action('cred_save_data', 'id_for_posts_rand_five_digit',10,2);
function id_for_posts_rand_five_digit($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==5908)
    {
            $five_digit_random_number = mt_rand(10000,99999);
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-referencia-del-bono', $five_digit_random_number, true);
    }
}
#1857319

Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL and form URL, where I can edit your custom PHP codes, I need to test and debug it in a live website. thanks

#1857703

My issue is resolved now. Thank you!