Hi
Is there a way to generate a unic number to a post when posting from frontend? This would serve as a kind of register, so the user posting the post can identify it later. This number should be visible in archive and single post view aswell.
Actually yes this is possible but it would be required to use a little custom code to do this. Is there a specific length of numbers you want to use ? e.g 4 digits or 5 digits
It should also be noted that if there isn't a specific length then the post id can be used since this is also a unique number as well.
Should you wish to generate the numbers differently please let me know and I should be able to write up some code to generate the random numbers for you.
What you need to do is add one of the code below to our Toolset custom coding section in Toolset -> Settings -> Custom Code. Remember to activate it. For both methods you will see I have the form id == 12, just change this to your form ID that you are using.
The next thing is the "wpcf-custom-field" you will need to change this to your custom field slug keeping the "wpcf-" prefix.
Method 1 - Random 5 digit number.
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']==12)
{
$five_digit_random_number = mt_rand(10000,99999);
// add it to saved post meta
add_post_meta($post_id, 'wpcf-custom-field', $five_digit_random_number, true);
}
}
Method 2 - Using the Post ID
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']==12)
{
// add it to saved post meta
add_post_meta($post_id, 'wpcf-custom-field', $post_id, true);
}
}