I have a WordPress site which has different sign up forms for different schools.
mywordpressite.com/school1
mywordpressite.com/school2
They have different questions. Behind the scene I am using WPForms and Pages to create these questionaire.
But there are some common information user have to fill in these questionaire like below. They have to be mandatory for all schools
first name
last name
dob
email
phone number
I am also using hidden fields that has to be same across schools.
Rest other questions can be totally different.
Now when user submit the sign up form, I am using a add_action to inform about it to separate system via a api call
add_action( ‘wpforms_process_complete’, ‘wpf_dev_process_complete’, 10, 4 );
In wpf_dev_process_complete, I need to access those manadatory common fields
/*
* This will fire at the very end of a (successful) form entry.
*
* @link https://wpforms.com/developers/wpforms_process_complete/
*
* @param array $fields Sanitized entry field values/properties.
* @param array $entry Original $_POST global.
* @param array $form_data Form data and settings.
* @param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite.
*/
function wpf_dev_process_complete( $fields, $entry, $form_data, $entry_id ) {
// Get the full entry object
$entry = wpforms()->entry->get( $entry_id );
// Fields are in JSON, so we decode to an array
$entry_fields = json_decode( $entry->fields, true );
$fnm = strtolower($entry_fields[64][ 'first' ]);
$mnm = strtolower($entry_fields[64][ 'middle' ]);
$lnm = strtolower($entry_fields[64][ 'last' ]);
$dob = date("Y-m-d", strtotime($entry_fields[55][ 'value' ]));
//submit above values to api call
}
I am stuck on these Ids because different schools WPForms will have different Ids and these Id in WPFForms elements are auto generated and I dont know a way to control them. I cannot hardcode a number 64 for name for example.
I am trying to find a generic way to access some common fields in WordPress across different pages.
Every time, I add a new school, dont want to change code to have a if else condition to find value of first name or dob or hidden field based on school.
2
Thank you @Richard for guidance. I did it like this
$id = $entry['id'];
$vfnm = "";
$vmnm = "";
$vlnm = "";
$vdob = "";
$vma = "";
$author_id = "";
// Get the full entry object
$entry = wpforms()->entry->get( $entry_id );
// Fields are in JSON, so we decode to an array
$entry_fields = json_decode( $entry->fields, true );
foreach ($entry_fields as $field_data) {
if(strtolower($field_data['name']) == "full legal name"){
$vfnm = strtolower($field_data['first']);
$vmnm = strtolower($field_data['middle']);
$vlnm = strtolower($field_data['last']);
}
if(strtolower($field_data['name']) == "date of birth"){
$vdob = date("Y-m-d", strtotime($field_data['value']));
}
if(strtolower($field_data['name']) == "email"){
$vma = strtolower($field_data['value']);
}
if(strtolower($field_data['name']) == "author_id"){
$author_id = $field_data['value'];
}
}