Skip Navigation

[Resolved] Set default value of non-toolset field on CRED save

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

Problem: I would like to use the cred_save_data API to set the value of a custom field that is not managed by Toolset.

Solution: You can use the cred_save_data hook to add or change custom field information after the post is saved, even if the fields are not managed by Types. Types custom field slugs begin with "wpcf-" in the database, but other non-Toolset fields do not have this prefix. So if your custom field slug is "field-slug", then the code will look like this:

add_action('cred_save_data', 'nonts_custom_field_value_action',10,2);
function nonts_custom_field_value_action($post_id, $form_data)
{
    $forms = array( 12345 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        update_post_meta( $post_id, 'field-slug', 'field value' );
    }
}

Change 12345 to match the numeric ID of this Form, change field-slug to match the slug of the custom field, and change field value to be the value you want to store in the custom field.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created 6 years, 4 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)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by randallH-3 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1084338

How can I set default value of non-toolset field on CRED save?

#1084666

You can use the cred_save_data hook to add or change custom field information after the post is saved, even if the fields are not managed by Types. Types custom field slugs begin with "wpcf-" in the database, but other non-Toolset fields do not have this prefix. So if your custom field slug is "field-slug", then the code will look like this:

add_action('cred_save_data', 'nonts_custom_field_value_action',10,2);
function nonts_custom_field_value_action($post_id, $form_data)
{
    $forms = array( 12345 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        update_post_meta( $post_id, 'field-slug', 'field value' );
    }
}

Change 12345 to match the numeric ID of this Form, change field-slug to match the slug of the custom field, and change field value to be the value you want to store in the custom field.

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/update_post_meta

#1087389

Thank you!