Skip Navigation

[Resolved] Need to auto-populate an field by a second field

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

Problem: I have two custom fields in a post type. When a Form is submitted to create posts, I would like to automatically populate one of the custom fields using information from the other custom field to generate an HTML link tag.

Solution: Use the cred_save_data hook to trigger your own custom code when a Form is submitted.

add_action('cred_save_data', 'tssupp_autopopulate_html',10,2);
function tssupp_autopopulate_html($post_id, $form_data)
{
  $forms = array( 123, 456 );
  $input_slug = 'your-input-field-slug';
  $destination_slug = 'your-destination-field-slug';
 
  // if this hook is fired when submitting any of the forms in $forms
  if ( in_array( $form_data['id'], $forms ) )
  {
    // if input field has content, build the HTML string and save it in destination field
    if (isset($_POST['wpcf-' . $input_slug]))
    {
      $input = $_POST['wpcf-' . $input_slug];
      $content = '<a href="' . $input . '">' . $input . '</a>';
      update_post_meta($post_id, 'wpcf-' . $destination_slug, $content);
    }
  }
}

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

This support ticket is created 3 years, 10 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 4 replies, has 3 voices.

Last updated by Florian 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1995815

I have a custom post type that contains custom fields. Among other fields, there is an ID field that will be filled by third party users. From this ID then a HTML must be generated, which contains the ID and is also stored in an additional field of the same field group.

E.G.: The user enters the ID 123456 in the "ID" field - the additional "Code" field then contains an associated HTML code that also contains the ID. For example (for testing) "ID"

We can't solve this via a view, unfortunately, so I'd have the answer ready - rather, it needs to be set within a record of the field group.

Can you give a hint on how this could be implemented?

#1995967

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You'd need custom code to implement this. Your options rather depend on how the users are entering the data. Is it in the backend, or using a frontend form?

If it's a frontend form then you can trigger some PHP code to update the second field based upon the value of the first with the cred_save_data hook, the documentation for which includes examples here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1995983

Frontend wouldn't be a problem, backend gives me a hard time. Users should fill in the field whereupon the second field is composed quasi automatically from the entered value and further parameters.

#1996517

Hi, you could use the Forms API cred_save_data to programmatically set a custom field value based on another custom field value, as Nigel suggested. This hook is fired after the Form is submitted, and gives you the ability to access those existing custom field values and create new ones. Ideally you would remove from the Form the custom field that will contain the generated HTML. Then you can use the following code as a template for crafting your own custom solution.

// automatically set a custom field value based on a different field value supplied in a form
// https://toolset.com/forums/topic/need-to-auto-populate-an-field-by-a-second-field/
add_action('cred_save_data', 'tssupp_autopopulate_html',10,2);
function tssupp_autopopulate_html($post_id, $form_data)
{
  $forms = array( 123, 456 );
  $input_slug = 'your-input-field-slug';
  $destination_slug = 'your-destination-field-slug';

  // if this hook is fired when submitting any of the forms in $forms
  if ( in_array( $form_data['id'], $forms ) )
  {
    // if input field has content, build the HTML string and save it in destination field
    if (isset($_POST['wpcf-' . $input_slug]))
    {
      $input = $_POST['wpcf-' . $input_slug];
      $content = '<a href="' . $input . '">' . $input . '</a>';
      update_post_meta($post_id, 'wpcf-' . $destination_slug, $content);
    }
  }
}

Replace 123, 456 with a comma-separated list of numeric Form IDs. Any time one of these Forms is submitted, the following code will be executed. Replace your-input-field-slug with the slug of the custom field that will contain the User-submitted content. Replace your-destination-field-slug with the slug of the custom field that will contain the generated HTML content. This line creates an HTML link using the User's submitted custom field content:

$content = '<a href="' . $input . '">' . $input . '</a>';

You can tinker with that line if you'd like to adjust the generated HTML, or rework this code completely to fit your needs. Note that Types field slugs in the DB include a wpcf- prefix that is not shown in wp-admin. So if the slug in wp-admin is user-email, then the slug in the postmeta, termmeta, or usermeta table will be wpcf-user-email. Any time you call WordPress functions that require a field slug, the wpcf- prefix is required. Types shortcodes and other Toolset APIs may not require the wpcf- prefix, so it's important to be aware of the syntax requirements as stated in our documentation. The cred_save_data documentation is here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1997091

My issue is resolved now. Thank you!