Skip Navigation

[Resolved] trying to automatically create a fields value

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

Problem:
trying to automatically create a fields value

Solution:
To generate such unique ID - you can use the Form's API hook: cred_save_data

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/trying-to-automatically-create-a-fields-value/#post-1347121

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 5 years, 1 month 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.

Our next available supporter will start replying to tickets in about 5.76 hours from now. Thank you for your understanding.

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 3 replies, has 3 voices.

Last updated by Chris 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1347107

Hi,

On creating a custom post type of 'school' I would like to automatically create a unique field of a 'school id' - which is made up of the school name (ie post title) with whitespace characters removed and truncated to xx (say 10) characters plus numeric id (which could be the post id, but does not have to be.

So with the post title "Anytown High School" the dynamically created id would look something like 'anytownhig123'

The posts will be created by users on the front end using a post form. The id should be created automatically but not be editable. It can then be displayed on the school post type using a content template.

I have searched the docs but haven't found much. Maybe this should be a hidden field? I'm not sure how to create the values.

Any suggestions very welcome

Many thanks

#1347121

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Assuming that you have created the field "'school id" using Types plugin.

To generate such unique ID - you can use the Form's API hook: cred_save_data

You should try to add the following code to your current theme's functions.php file:

add_action("cred_save_data", "func_create_unique_post_slug",10,2);
function func_create_unique_post_slug($post_id, $form_data) {   
 
if ($form_data["id"]==9999) {
     
             $unique_id =  trim($_POST['post_title']).$post_id;
              update_post_meta( $post_id, 'wpcf-school id', $unique_id );
    }
}

Where:
- Replace 9999 with your original form ID

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1347133

The DOC does not show how to do this in one piece because it is not something Toolset does in the visible GUI.

You can achieve the logic by applying some Custom Code to Toolset Form's filters. You might be familiar with the save_post() hook of WordPress for when you want to alter the Post when saving a post. Toolset Forms provides a similar API. I'll explain below how you can achieve this.

1. Create the Toolset Form that adds those Posts, and include all fields you want the user to fill in when creating the post (or editing it)
2. Create a Custom Field with Types (not required, single line) that will be named School ID or similar. Important here, save the slug for later usage. Do not include this Field in the Toolset form (it will be included by default, hence remove it)
3. Consult https://codex.wordpress.org/Function_Reference/update_post_meta and https://developer.wordpress.org/reference/functions/get_the_title/ to get and update post data in WordPress, additionally to hidden link (to Truncate). Craft a working code that gets the post title by ID and updates to a field with slug as in #2 the new value made up of truncated title and post id.
4. Now consult the DOC for Toolset Form's save_post replacement: cred_save_data: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
You can practically insert your existing code from #3 above, by slightly altering the Post ID (in this hook you can use $post_id) and you will need to wrap some checks for being on the right form.

Below is a working example, based on a Single Line field school-id, and a form with ID 10.
Note that it does not include the sanitization as in truncating and escaping empty characters or others, as that is custom code we cannot assist, however above DOC of PHP should help here.

1. Created the Form for the post, and the field with slug school-id, which is not added to the form
2. Insert this custom snippet in your functions.php file:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==10)//Change form id as per your requirements 
    {
       $post_title = get_the_title( $post_id );
       $school_id = $post_title . '-' . $post_id;
       update_post_meta($post_id, 'wpcf-school-id', $school_id);//Important here is to add wpcf-prefx
    }
    }
}

Now, when the user submits the form with ID 10, the code will get the post title, its ID, concatenate it with a -, update a field school-id and resume.

I hope this helps to build the rest of the logic on top of it!

NOTE:
It's justified to ask why not to simply put ShortCodes that return the Post ID and Title in a hidden Field in the Form, and just submit it.
The reason is, WordPress dislikes nested ShortCodes or better, apostrophes (and hence HTML or other content) inside ShortCodes, so it's adequate to use Custom PHP in this case.

#1348879

All sorted now - thank you.