Skip Navigation

[Resolved] Cred save data on existing field

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

Problem: I would like to capture the value from a generic field using cred_save_data, and store it in a custom field.

Solution: Add the following code to functions.php:

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
        if (isset($_POST['responsable-projet']))
        {
            update_post_meta($post_id, 'wpcf-responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

Create a generic field called 'responsable-projet' in your CRED form, and create a custom field called 'responsable-projet' in the post type associated with the CRED form.

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 18 replies, has 2 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#555241

Types adds 'wpcf-' at the beginning of all field names in the database. You should use the wpcf- prefix in the update_post_meta call if the 'responsable-projet' field is a Types custom field:

update_post_meta($post_id, 'wpcf-responsable-projet', $_POST['responsable-projet'], true);
#555244

Now it works !
Thank you.

Still, it registers the position of the "responsable-projet" in the list... it should register the name instead. How could I do this ?
Thank you.

#555252

You could change the options of your generic field to include the role name instead of the role ID in each value. value is what will be saved in your custom field.

#555262

It seems it's impossible to register the display name some some reason, but it worked great with user_login !
Thank you very much for your help !