Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created January 21, 2026 06:29
Show Gist options
  • Select an option

  • Save xlplugins/e49ec4be3d75b1c88dcad3d2b648d2d9 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/e49ec4be3d75b1c88dcad3d2b648d2d9 to your computer and use it in GitHub Desktop.
optin conversion through webhook youcanbookme.com
<?php
/**
* Plugin Name: You can book me webhook endpoint
* Description: Receives webhook from YouCanBookMe and saves as optin conversion
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'rest_api_init', function () {
register_rest_route(
'you-can-book-me/v1',
'/webhook',
array(
'methods' => 'POST',
'callback' => 'ycbm_webhook_handler',
'permission_callback' => '__return_true',
)
);
} );
function ycbm_webhook_handler( WP_REST_Request $request ) {
if ( ! class_exists( 'WFFN_Core' ) ) {
return array( 'success' => false, 'message' => 'FunnelKit not active' );
}
$json_body = $request->get_json_params();
if ( empty( $json_body ) ) {
return array( 'success' => false, 'message' => 'No data received' );
}
// Extract fields from webhook
$name = isset( $json_body['name'] ) ? sanitize_text_field( $json_body['name'] ) : '';
$email = isset( $json_body['email'] ) ? sanitize_email( $json_body['email'] ) : '';
$phone = isset( $json_body['phone'] ) ? sanitize_text_field( $json_body['phone'] ) : '';
if ( empty( $email ) ) {
WFFN_Core()->logger->log( 'YCBM Webhook: No email provided', 'ycbm-webhook', true );
return array( 'success' => false, 'message' => 'Email is required' );
}
$wffn_optin_id = 18836;
// Map webhook fields to optin fields
$wffn_posted_data = array(
'optin_first_name' => $name,
'email' => $email,
'optin_phone' => $phone,
);
$form_data = array();
$form_data['step_id'] = $wffn_optin_id;
$funnel_id = get_post_meta( $form_data['step_id'], '_bwf_in_funnel', true );
if ( empty( $funnel_id ) ) {
WFFN_Core()->logger->log( 'YCBM Webhook: No funnel found for optin ID ' . $wffn_optin_id, 'ycbm-webhook', true );
return array( 'success' => false, 'message' => 'Invalid optin page ID' );
}
$form_data['funnel_id'] = $funnel_id;
// Get current user
global $current_user;
$user_id = ( ! empty( $current_user->ID ) && ( $current_user->ID > 0 ) ) ? $current_user->ID : 0;
// Create or get BWF contact
$bwf_contact = ( function_exists( 'bwf_get_contact' ) ) ? bwf_get_contact( $user_id, $email ) : new stdClass();
if ( $bwf_contact instanceof WooFunnels_Contact ) {
if ( 0 === $bwf_contact->get_id() ) {
$bwf_contact->set_status( 0 );
$bwf_contact->set_email( $email );
}
if ( ! empty( $name ) ) {
$bwf_contact->set_f_name( $name );
}
if ( ! empty( $phone ) ) {
$bwf_contact->set_contact_no( $phone );
}
$bwf_contact->save( true );
$form_data['cid'] = $bwf_contact->get_id();
}
$form_data['email'] = $email;
// Insert optin record
$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 );
if ( is_numeric( $last_id ) && $last_id > 0 ) {
$tracking_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, $tracking_data );
}
WFFN_Core()->logger->log( 'YCBM Webhook: Optin saved successfully. Email: ' . $email . ', Entry ID: ' . $last_id, 'ycbm-webhook', true );
return array( 'success' => true, 'entry_id' => $last_id );
}
WFFN_Core()->logger->log( 'YCBM Webhook: Failed to save optin for email: ' . $email, 'ycbm-webhook', true );
return array( 'success' => false, 'message' => 'Failed to save optin' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment