Skip Navigation

[Resolved] Display field on CRED ‘edit existing’ form but don’t display the fields contents

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

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 support ticket is created 5 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 7 replies, has 2 voices.

Last updated by Christian Cox 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1174304

Tell us what you are trying to do?
I would like to display a field on an 'edit existing content' form, but not show the data stored in the filed. I want the filed to be empty of data. So that the user can fill it in and overwrite what is currently stored in that field.

Is there any documentation that you are following?
no

Is there a similar example that we can see?
hidden link and click on the link "Update This Meeting"

What is the link to your site?
coda.flywheelsites.com

#1174837

Hi, 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, it 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.
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api
I can help with that code if you need assistance.

#1177438

Hi Christian,

Thanks for the detailed response!

Yes it would be super if you would please help with the code.

Thanks in advance for your assistance.

#1178518

Sure, can you tell me:
- What is the numeric ID of this edit Form?
- What is the slug of the custom field you want to overwrite?
- What is the slug of the generic field you have added to capture the new value?
- If the User does not provide a value for the field, should the Form leave the field as it was set before the edit, or delete the existing custom field value?

#1178532

Hi Christian,

yes, details you asked:
- Form numeric ID: 8167
- Slug of the custom field: submitter_email and submitter_title
- Generic field: offer_email and offer_title
- If the User does not provide a value for the field, the Form should leave the field as it was set before the edit.

Thanks.

#1178645

Please add this code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom Code:

// 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'] );
    }
  }
}

Let me know if the results aren't what you'd expect.

#1183370

Hi Christian,

Your code is fine.

You can close this topic.

Thanks

#1183689

Closing per your request, thanks.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.