Skip to content

Instantly share code, notes, and snippets.

@slaFFik
Created January 23, 2020 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaFFik/d43ba58e19ade4d76ded3b0e34082472 to your computer and use it in GitHub Desktop.
Save slaFFik/d43ba58e19ade4d76ded3b0e34082472 to your computer and use it in GitHub Desktop.
WPForms: merge values for 2 fields into a 3rd one
<?php
add_filter( 'wpforms_entry_save_data', static function ( $fields, $entry, $form_data ) {
$value1 = '';
$value2 = '';
// These are field IDs to take data from, and to merge into.
$field1_id = 1;
$field2_id = 2;
$merged_id = 3;
foreach ( $fields as $field ) {
if ( $field['id'] === $field1_id ) {
$value1 = $field['value'];
}
if ( $field['id'] === $field2_id ) {
$value2 = $field['value'];
}
}
$fields[ $merged_id ]['value'] = $value1 . ' - ' . $value2;
return $fields;
}, 10, 3 );
@shabinvp
Copy link

Hi
this is very helpful, since the field ID is not unique in wpforms is there a way to validate the filter with form ID ?

@slaFFik
Copy link
Author

slaFFik commented May 20, 2023

Yes, you can check the $form_data['id'] to be the value you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment