Problem: I have an edit post Form that includes a custom field. I would like to make the field empty when the Form loads. If the User adds a value to this field, I want to save that value as the custom field value. However, if the User does not add a value to this field I want to keep the original field value.
Solution:
It depends on the type of field and settings. In some simple cases, you can edit the Form code and add the value attribute to the field shortcode. Set the value to be an empty space, like this:
[cred_field field="some-field" force_type="field" class="form-control" output="bootstrap" value=" "]
However, if it's a numeric field this will show a validation error if the User does not modify the value on the front-end, because an empty space is not a number. That can be confusing, and there are some other quirks so the most foolproof way to set this up is to delete the actual field from the Form. Add a generic field to capture a new value, then use the Forms API to overwrite the existing field value with the new field value. Here is an example:
// overwrite the values of submitter custom fields if the User supplies them in the generic fields of Form 8167 add_action('cred_save_data', 'overwrite_submitter_fields_action',10,2); function overwrite_submitter_fields_action($post_id, $form_data) { // if a specific form if ($form_data['id']==8167) { if( isset($_POST['offer_email']) && trim($_POST['offer_email']) != '' ) { update_post_meta( $post_id, 'wpcf-submitter_email', $_POST['offer_email'] ); } if( isset($_POST['offer_title']) && trim($_POST['offer_title']) != '' ) { update_post_meta( $post_id, 'wpcf-submitter_title', $_POST['offer_title'] ); } } }
Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api
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 7 replies, has 2 voices.
Last updated by 6 years, 1 month ago.
Assisted by: Christian Cox.