Created
October 1, 2025 12:04
-
-
Save annuman97/82e36beb494e4a3983a455ea153d3c9e to your computer and use it in GitHub Desktop.
Trigger Webhook after confirming the double opt-in in Fluent Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Handling the fluent form entry confirmation hook that is triggered on double optin confirmation | |
| * for forms that have that option enabled (this part is coded as webhook UI for fluent forms triggers | |
| * upon form submission, before the user as double opted in. This is the only way | |
| * to access the confirmation hook. | |
| * | |
| * This module will trigger 2 webhooks : | |
| * 1-to add the email to the encharge subscriber list | |
| * 2-to create or update a contact in one page CRM for the subcriber | |
| * 3-to add a note in the contact that they downloaded the 7FreeTipsPosRelDis ebook | |
| */ | |
| add_action('fluentform/entry_confirmation', function ($request) { | |
| // CHANGE: The confirmation request provides 'ff_landing' (form id) and 'entry_confirmation' (hash). | |
| // Older code incorrectly expected 'form_id' and 'email' directly in $request. | |
| if (empty($request['ff_landing']) || empty($request['entry_confirmation'])) { | |
| return; | |
| } | |
| // CHANGE: Read the correct keys from the request payload. | |
| $form_id = (int) $request['ff_landing']; | |
| $hash = sanitize_text_field($request['entry_confirmation']); | |
| // OPTIONAL: Run only for a specific form (adjust the ID as needed). | |
| $TARGET_FORM_ID = 2; | |
| if ($form_id !== $TARGET_FORM_ID) { | |
| return; | |
| } | |
| // CHANGE: Resolve the confirmation hash to a real submission ID via FF meta table (_entry_uid_hash). | |
| global $wpdb; | |
| $meta_table = $wpdb->prefix . 'fluentform_submission_meta'; | |
| $submission_id = (int) $wpdb->get_var($wpdb->prepare( | |
| "SELECT submission_id FROM {$meta_table} | |
| WHERE form_id = %d AND meta_key = %s AND value = %s LIMIT 1", | |
| $form_id, '_entry_uid_hash', $hash | |
| )); | |
| if (!$submission_id) { | |
| error_log('FF: entry_confirmation — submission not found for hash'); | |
| return; | |
| } | |
| // SAFETY: Ensure model class exists (Pro is active). | |
| if (!class_exists('\FluentForm\App\Models\Submission')) { | |
| return; | |
| } | |
| // CHANGE: Load the confirmed submission and extract field data from the JSON response. | |
| $submission = \FluentForm\App\Models\Submission::with('form')->find($submission_id); | |
| if (!$submission) { | |
| return; | |
| } | |
| $data = json_decode($submission->response, true) ?: []; | |
| // NOTE: Adjust these field keys to match your form (e.g., 'email', 'first_name', 'last_name'). | |
| $email = isset($data['email']) ? sanitize_email($data['email']) : ''; | |
| $first_name = isset($data['first_name']) ? sanitize_text_field($data['first_name']) : ''; | |
| $last_name = isset($data['last_name']) ? sanitize_text_field($data['last_name']) : ''; | |
| // -------- 1) Encharge Webhook -------- | |
| $encharge_payload = [ | |
| 'email' => $email, | |
| 'form_id' => $form_id, | |
| 'entry_id' => $submission_id, | |
| ]; | |
| // PLACEHOLDER: Replace with your actual Encharge hook URL. | |
| wp_remote_post('https://api.encharge.io/v1/hooks/REPLACE_WITH_YOUR_HOOK_ID', [ | |
| 'headers' => ['Content-Type' => 'application/json'], | |
| 'body' => wp_json_encode($encharge_payload), | |
| 'timeout' => 20, | |
| ]); | |
| // -------- 2) OnePageCRM: search/create/update + note -------- | |
| // PLACEHOLDER: Replace with your real OnePageCRM credentials. | |
| $api_user = 'YOUR_ONEPAGECRM_USER'; | |
| $api_key = 'YOUR_ONEPAGECRM_TOKEN'; | |
| if ($email) { | |
| // Search existing contact | |
| $search_url = 'https://app.onepagecrm.com/api/v3/contacts/search?term=' . rawurlencode($email); | |
| $search_res = wp_remote_get($search_url, [ | |
| 'headers' => [ | |
| 'Accept' => 'application/json', | |
| 'X-OnePageCRM-User' => $api_user, | |
| 'X-OnePageCRM-Token' => $api_key, | |
| ], | |
| 'timeout' => 20, | |
| ]); | |
| $search_body = json_decode(wp_remote_retrieve_body($search_res), true); | |
| $contact_id = !empty($search_body['contacts'][0]['id']) ? $search_body['contacts'][0]['id'] : false; | |
| if (!$contact_id) { | |
| // Create new contact as Lead | |
| $create_url = 'https://app.onepagecrm.com/api/v3/contacts'; | |
| $create_body = [ | |
| 'contact' => [ | |
| 'first_name' => $first_name, | |
| 'last_name' => $last_name, | |
| 'emails' => [['type' => 'work', 'value' => $email]], | |
| 'status' => 'Lead', | |
| ] | |
| ]; | |
| $create_res = wp_remote_post($create_url, [ | |
| 'headers' => [ | |
| 'Content-Type' => 'application/json', | |
| 'Accept' => 'application/json', | |
| 'X-OnePageCRM-User' => $api_user, | |
| 'X-OnePageCRM-Token' => $api_key, | |
| ], | |
| 'body' => wp_json_encode($create_body), | |
| 'timeout' => 20, | |
| ]); | |
| $created = json_decode(wp_remote_retrieve_body($create_res), true); | |
| $contact_id = $created['contact']['id'] ?? false; | |
| } else { | |
| // Update status to Lead | |
| $update_url = "https://app.onepagecrm.com/api/v3/contacts/{$contact_id}"; | |
| wp_remote_request($update_url, [ | |
| 'method' => 'PATCH', | |
| 'headers' => [ | |
| 'Content-Type' => 'application/json', | |
| 'Accept' => 'application/json', | |
| 'X-OnePageCRM-User' => $api_user, | |
| 'X-OnePageCRM-Token' => $api_key, | |
| ], | |
| 'body' => wp_json_encode(['status' => 'Lead']), | |
| 'timeout' => 20, | |
| ]); | |
| } | |
| // Add a note to the contact | |
| if ($contact_id) { | |
| $note_url = "https://app.onepagecrm.com/api/v3/contacts/{$contact_id}/notes"; | |
| $note = [ | |
| 'note' => [ | |
| 'text' => 'Ebook downloaded via website popup on ' . current_time('mysql') . '.', | |
| ] | |
| ]; | |
| wp_remote_post($note_url, [ | |
| 'headers' => [ | |
| 'Content-Type' => 'application/json', | |
| 'Accept' => 'application/json', | |
| 'X-OnePageCRM-User' => $api_user, | |
| 'X-OnePageCRM-Token' => $api_key, | |
| ], | |
| 'body' => wp_json_encode($note), | |
| 'timeout' => 20, | |
| ]); | |
| } | |
| } | |
| // Optional: log for debugging | |
| error_log("FF: entry_confirmation OK | form {$form_id} | entry {$submission_id} | email {$email}"); | |
| }, 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment