Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active July 15, 2025 09:58
Show Gist options
  • Save xlplugins/2b4a2d303d3d7a20553366b2f015a3e4 to your computer and use it in GitHub Desktop.
Save xlplugins/2b4a2d303d3d7a20553366b2f015a3e4 to your computer and use it in GitHub Desktop.
WSForm use as Optin form step
add_action('wsf_submit_post_complete', 'wffn_save_wsform_data_in_optin', 10, 1);
/**
* Handles WS Form submission and maps form fields to FunnelKit optin fields dynamically.
*
* This function hooks into the 'wsf_submit_post_complete' action provided by WS Form. It dynamically maps
* WS Form fields to logical optin fields (first name, last name, email, phone) by inspecting the form's
* structure and matching on field type and label. This approach is robust to field reordering and renaming.
*
* @param WS_Form_Submit $submit The WS Form submission object passed by the action hook.
* @return void
*/
function wffn_save_wsform_data_in_optin($submit)
{
// Get all post data as a flat array (field meta key => value)
$data = $submit->get_post_data();
$wffn_optin_id = 00000; //optin page ID
// Ensure the form object is available
if (empty($submit->form_object) || empty($submit->form_object->groups)) {
error_log('WS Form: form_object or groups missing in submission.');
return;
}
$form_object = $submit->form_object;
// Build a map: logical_name => field_key (e.g., 'email' => 'field_3')
$field_map = array();
// Iterate over all groups, sections, and fields in the form
foreach ($form_object->groups as $group) {
if (empty($group->sections)) {
continue;
}
foreach ($group->sections as $section) {
if (empty($section->fields)) {
continue;
}
foreach ($section->fields as $field) {
// Defensive: ensure label and type are set
$label = isset($field->label) ? strtolower(trim($field->label)) : '';
$type = isset($field->type) ? strtolower($field->type) : '';
$field_id = isset($field->id) ? $field->id : null;
if (!$field_id) {
continue;
}
$meta_key = 'field_' . $field_id;
// Map by type or label (case-insensitive, robust to label changes)
if ($type === 'email' || strpos($label, 'email') !== false) {
$field_map['email'] = $meta_key;
} elseif ($type === 'tel' || strpos($label, 'phone') !== false) {
$field_map['phone'] = $meta_key;
} elseif (strpos($label, 'first name') !== false) {
$field_map['first_name'] = $meta_key;
} elseif (strpos($label, 'last name') !== false) {
$field_map['last_name'] = $meta_key;
}
// Extend here for more mappings as needed (e.g., company, address, etc.)
}
}
}
// Map WS Form data to FunnelKit optin fields using the dynamic field map
$wffn_posted_data = array(
'optin_first_name' => isset($field_map['first_name']) && isset($data[$field_map['first_name']]) ? $data[$field_map['first_name']] : '',
'optin_last_name' => isset($field_map['last_name']) && isset($data[$field_map['last_name']]) ? $data[$field_map['last_name']] : '',
'email' => isset($field_map['email']) && isset($data[$field_map['email']]) ? $data[$field_map['email']] : '',
'optin_phone' => isset($field_map['phone']) && isset($data[$field_map['phone']]) ? $data[$field_map['phone']] : '',
);
if (empty($wffn_posted_data['email'])) {
return;
}
$form_data = [];
$form_data['step_id'] = $wffn_optin_id;
$funnel_id = get_post_meta($form_data['step_id'], '_bwf_in_funnel', true);
if (empty($funnel_id)) {
return;
}
$form_data['funnel_id'] = $funnel_id;
global $current_user;
$user_id = (! empty($current_user->ID) && ($current_user->ID > 0)) ? $current_user->ID : 0;
$bwf_contact = (function_exists('bwf_get_contact')) ? bwf_get_contact($user_id, $wffn_posted_data['email']) : new stdClass();
if ($bwf_contact instanceof WooFunnels_Contact) {
if (0 === $bwf_contact->get_id()) {
$bwf_contact->set_status(0);
$bwf_contact->set_email($wffn_posted_data['email']);
}
if (isset($wffn_posted_data['optin_first_name'])) {
$bwf_contact->set_f_name($wffn_posted_data['optin_first_name']);
$bwf_contact->set_f_name('');
}
$bwf_contact->save(true);
$form_data['cid'] = $bwf_contact->get_id();
}
$form_data['email'] = $wffn_posted_data['email'];
/** Creating bwf_contact **/
if ($wffn_posted_data['email'] && $wffn_posted_data['email'] !== '') {
$form_data['email'] = $wffn_posted_data['email'];
$form_data['data'] = $wffn_posted_data;
unset($form_data['data']['email']);
$get_hash = WFFN_Core()->data->generate_transient_key();
$form_data['opid'] = $get_hash;
$last_id = WFFN_DB_Optin::get_instance()->insert_optin($form_data);
WFFN_Core()->logger->log(' form name 7 ' . print_r($wffn_posted_data, true), 'wffn-xx', true);
if (is_numeric($last_id) && $last_id > 0) {
$wffn_posted_data = array(
'cid' => $form_data['cid'],
'optin_entry_id' => $last_id
);
if (class_exists('BWF_Ecomm_Tracking_Common') && method_exists('BWF_Ecomm_Tracking_Common', 'update_optin_tracking_data')) {
BWF_Ecomm_Tracking_Common::get_instance()->update_optin_tracking_data($wffn_optin_id, $wffn_posted_data);
}
WFFN_Core()->logger->log(' form name 8 ' . print_r($wffn_posted_data, true), 'wffn-xx', true);
WFFN_Core()->logger->log('Optin form save successfully: ' . print_r($form_data['email'], true), 'wffn_test', true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment