Spin Wheel

⌘K
  1. Home
  2. Spin Wheel
  3. Hooks & Filters (Deve...
  4. After database insert (uspw_after_entry_saved)

After database insert (uspw_after_entry_saved)

uspw_after_entry_saved

Description

Fires after an entry is successfully saved to the database. Perfect for CRM synchronization, webhook triggers, and external integrations that need the database entry ID.

Hook Type: Action
Since: 1.0.0

Parameters

ParameterTypeDescription
$entry_idintThe database entry ID (auto-increment)
$dataarrayComplete entry data that was saved
$campaign_idintThe ID of the spin wheel campaign

Usage

add_action( 'uspw_after_entry_saved', 'my_post_save_handler', 10, 3 );
function my_post_save_handler( $entry_id, $data, $campaign_id ) {
    // Your custom logic here
}

Real-World Examples

Example 1: Sync with CRM (Salesforce)

Automatically create or update leads in Salesforce when entries are saved.

add_action( 'uspw_after_entry_saved', 'sync_to_salesforce', 10, 3 );
function sync_to_salesforce( $entry_id, $data, $campaign_id ) {
    $sf_instance = get_option( 'salesforce_instance_url' );
    $sf_token = get_option( 'salesforce_access_token' );
    
    if ( ! $sf_instance || ! $sf_token ) {
        return;
    }
    
    // Prepare lead data
    $lead_data = [
        'FirstName' => $data['name'] ?? '',
        'LastName' => '', // Parse from full name if needed
        'Email' => $data['email'] ?? '',
        'Phone' => $data['phone'] ?? '',
        'Company' => $data['company'] ?? 'Unknown',
        'LeadSource' => 'Spin Wheel',
        'Description' => sprintf(
            'Won %s (Code: %s) from Campaign %d',
            $data['coupon_title'] ?? 'a prize',
            $data['coupon_code'] ?? 'N/A',
            $campaign_id
        ),
        'Spin_Wheel_Entry_ID__c' => $entry_id, // Custom field
        'Prize_Value__c' => $data['prize_value'] ?? 0,
        'Coupon_Code__c' => $data['coupon_code'] ?? '',
        'Campaign_ID__c' => $campaign_id
    ];
    
    // Create lead in Salesforce
    $response = wp_remote_post( $sf_instance . '/services/data/v52.0/sobjects/Lead', [
        'headers' => [
            'Authorization' => 'Bearer ' . $sf_token,
            'Content-Type' => 'application/json'
        ],
        'body' => wp_json_encode( $lead_data )
    ] );
    
    if ( ! is_wp_error( $response ) ) {
        $result = json_decode( wp_remote_retrieve_body( $response ), true );
        
        if ( ! empty( $result['id'] ) ) {
            // Store Salesforce ID in entry meta
            global $wpdb;
            $meta_table = $wpdb->prefix . 'wdengage_entry_meta';
            $wpdb->insert( $meta_table, [
                'entry_id' => $entry_id,
                'meta_key' => 'salesforce_lead_id',
                'meta_value' => $result['id']
            ] );
        }
    }
}

Example 2: Trigger Zapier Webhook

Send entry data to Zapier for workflow automation.

add_action( 'uspw_after_entry_saved', 'trigger_zapier_webhook', 10, 3 );
function trigger_zapier_webhook( $entry_id, $data, $campaign_id ) {
    $zapier_webhook_url = get_option( 'spin_wheel_zapier_webhook' );
    
    if ( ! $zapier_webhook_url ) {
        return;
    }
    
    // Prepare payload
    $payload = [
        'entry_id' => $entry_id,
        'campaign_id' => $campaign_id,
        'campaign_name' => get_the_title( $campaign_id ),
        'email' => $data['email'] ?? '',
        'name' => $data['name'] ?? '',
        'phone' => $data['phone'] ?? '',
        'company' => $data['company'] ?? '',
        'prize_label' => $data['coupon_title'] ?? '',
        'prize_value' => $data['prize_value'] ?? 0,
        'coupon_code' => $data['coupon_code'] ?? '',
        'status' => $data['status'] ?? '',
        'created_at' => $data['created_at'] ?? current_time( 'mysql' ),
        'ip_address' => $data['ip_address'] ?? '',
        'country' => $data['country'] ?? '',
        'utm_source' => $data['utm_source'] ?? '',
        'utm_campaign' => $data['utm_campaign'] ?? ''
    ];
    
    // Send to Zapier (async to avoid blocking)
    wp_remote_post( $zapier_webhook_url, [
        'blocking' => false, // Don't wait for response
        'body' => wp_json_encode( $payload ),
        'headers' => [ 'Content-Type' => 'application/json' ]
    ] );
}

Example 3: Create WooCommerce Customer

Automatically create a WooCommerce customer account for new winners.

add_action( 'uspw_after_entry_saved', 'create_woocommerce_customer', 10, 3 );
function create_woocommerce_customer( $entry_id, $data, $campaign_id ) {
    // Only for WooCommerce-enabled sites
    if ( ! class_exists( 'WooCommerce' ) ) {
        return;
    }
    
    $email = $data['email'] ?? '';
    
    if ( empty( $email ) ) {
        return;
    }
    
    // Check if customer already exists
    $customer_id = email_exists( $email );
    
    if ( ! $customer_id ) {
        // Create new customer
        $customer_id = wc_create_new_customer( $email, '', wp_generate_password() );
        
        if ( is_wp_error( $customer_id ) ) {
            error_log( 'Failed to create WooCommerce customer: ' . $customer_id->get_error_message() );
            return;
        }
    }
    
    // Update customer meta
    if ( $customer_id ) {
        update_user_meta( $customer_id, 'first_name', $data['name'] ?? '' );
        update_user_meta( $customer_id, 'billing_email', $email );
        update_user_meta( $customer_id, 'billing_phone', $data['phone'] ?? '' );
        update_user_meta( $customer_id, 'spin_wheel_winner', true );
        update_user_meta( $customer_id, 'spin_wheel_entry_id', $entry_id );
        update_user_meta( $customer_id, 'spin_wheel_coupon', $data['coupon_code'] ?? '' );
        
        // Add customer note
        $customer = new WC_Customer( $customer_id );
        $customer->add_meta_data( 'spin_wheel_note', sprintf(
            'Won %s (value: $%s) from Spin Wheel Campaign #%d',
            $data['coupon_title'] ?? 'a prize',
            $data['prize_value'] ?? '0',
            $campaign_id
        ), true );
        $customer->save();
        
        // Store WooCommerce customer ID in entry meta
        global $wpdb;
        $meta_table = $wpdb->prefix . 'wdengage_entry_meta';
        $wpdb->insert( $meta_table, [
            'entry_id' => $entry_id,
            'meta_key' => 'wc_customer_id',
            'meta_value' => $customer_id
        ] );
    }
}

Example 4: Send to Google Sheets

Log all entries to a Google Sheet for easy reporting and analysis.

add_action( 'uspw_after_entry_saved', 'log_to_google_sheets', 10, 3 );
function log_to_google_sheets( $entry_id, $data, $campaign_id ) {
    $sheet_webhook = get_option( 'spin_wheel_google_sheets_webhook' );
    
    if ( ! $sheet_webhook ) {
        return;
    }
    
    // Prepare row data
    $row_data = [
        'Entry ID' => $entry_id,
        'Date/Time' => $data['created_at'] ?? current_time( 'mysql' ),
        'Campaign ID' => $campaign_id,
        'Campaign Name' => get_the_title( $campaign_id ),
        'Name' => $data['name'] ?? '',
        'Email' => $data['email'] ?? '',
        'Phone' => $data['phone'] ?? '',
        'Company' => $data['company'] ?? '',
        'Prize' => $data['coupon_title'] ?? '',
        'Prize Value' => $data['prize_value'] ?? 0,
        'Coupon Code' => $data['coupon_code'] ?? '',
        'Status' => $data['status'] ?? '',
        'Country' => $data['country'] ?? '',
        'City' => $data['city'] ?? '',
        'IP Address' => $data['ip_address'] ?? '',
        'UTM Source' => $data['utm_source'] ?? '',
        'UTM Medium' => $data['utm_medium'] ?? '',
        'UTM Campaign' => $data['utm_campaign'] ?? '',
        'Lead Score' => $data['lead_score'] ?? 0
    ];
    
    // Send to Google Sheets via webhook (Google Apps Script)
    wp_remote_post( $sheet_webhook, [
        'blocking' => false,
        'body' => wp_json_encode( $row_data ),
        'headers' => [ 'Content-Type' => 'application/json' ]
    ] );
}

Example 5: Queue Background Jobs

Add entries to a job queue for asynchronous processing.

add_action( 'uspw_after_entry_saved', 'queue_background_jobs', 10, 3 );
function queue_background_jobs( $entry_id, $data, $campaign_id ) {
    // Job 1: Email enrichment (find social profiles, company info)
    if ( ! empty( $data['email'] ) ) {
        wp_schedule_single_event( time() + 60, 'spin_wheel_enrich_email', [
            'entry_id' => $entry_id,
            'email' => $data['email']
        ] );
    }
    
    // Job 2: Send to multiple CRMs (HubSpot, Salesforce, Pipedrive)
    wp_schedule_single_event( time() + 30, 'spin_wheel_sync_crms', [
        'entry_id' => $entry_id,
        'data' => $data,
        'campaign_id' => $campaign_id
    ] );
    
    // Job 3: Generate and send personalized follow-up email (delayed by 1 hour)
    if ( ! empty( $data['email'] ) && $data['status'] === 'won' ) {
        wp_schedule_single_event( time() + 3600, 'spin_wheel_send_followup', [
            'entry_id' => $entry_id,
            'email' => $data['email'],
            'name' => $data['name'] ?? '',
            'coupon_code' => $data['coupon_code'] ?? ''
        ] );
    }
    
    // Job 4: Update analytics dashboard
    wp_schedule_single_event( time() + 120, 'spin_wheel_update_analytics', [
        'campaign_id' => $campaign_id,
        'entry_id' => $entry_id
    ] );
    
    // Store job queue info in entry meta
    global $wpdb;
    $meta_table = $wpdb->prefix . 'wdengage_entry_meta';
    $wpdb->insert( $meta_table, [
        'entry_id' => $entry_id,
        'meta_key' => 'background_jobs_queued',
        'meta_value' => wp_json_encode( [
            'email_enrichment' => time() + 60,
            'crm_sync' => time() + 30,
            'followup_email' => time() + 3600,
            'analytics_update' => time() + 120
        ] )
    ] );
}

// Register the background job handlers
add_action( 'spin_wheel_enrich_email', 'handle_email_enrichment', 10, 1 );
function handle_email_enrichment( $args ) {
    // Call Clearbit, FullContact, or similar API
    // Update entry with enriched data
}

add_action( 'spin_wheel_sync_crms', 'handle_crm_sync', 10, 1 );
function handle_crm_sync( $args ) {
    // Sync to multiple CRMs
}

add_action( 'spin_wheel_send_followup', 'handle_followup_email', 10, 1 );
function handle_followup_email( $args ) {
    // Send personalized follow-up email
}

add_action( 'spin_wheel_update_analytics', 'handle_analytics_update', 10, 1 );
function handle_analytics_update( $args ) {
    // Update custom analytics dashboard
}

Notes

  • This hook fires after the database insert is complete and $entry_id is available.
  • Perfect for external integrations, CRM sync, and any operations that require the database entry ID.
  • Consider using asynchronous processing (wp_schedule_single_event, background jobs) for time-consuming operations to avoid slowing down the user experience.
  • The $data array contains all saved entry data including custom fields added via uspw_before_save_entry.

How can we help?