Audio Preview Pro – Hook Examples

Get Started

Admin Hooks Examples

Modify Audio Data Before Display

// Add custom audio processing before displaying in admin
add_filter('wcap_pro_admin_audio_data', function($audio_data, $product_id) {
    // Add custom fields or modify existing data
    if (isset($audio_data['wcap_audio_urls'])) {
        foreach ($audio_data['wcap_audio_urls'] as $index => $url) {
            // Process URLs, add CDN prefixes, etc.
            $audio_data['wcap_audio_urls'][$index] = process_audio_url($url);
        }
    }
    return $audio_data;
}, 10, 2);

Add Custom Content to Meta Box

// Add content before the audio meta box
add_action('wcap_pro_admin_before_meta_box', function($post) {
    if ($post->post_type === 'product') {
        echo '<div class="custom-notice">Custom instructions for audio uploads</div>';
    }
});

// Add content after the audio meta box
add_action('wcap_pro_admin_after_meta_box', function($post) {
    echo '<div class="custom-tips">Pro tip: Use 30-60 second previews for best results</div>';
});

Custom Audio Validation

// Validate audio URLs before saving
add_filter('wcap_pro_save_audio_urls', function($urls, $product_id) {
    $validated_urls = array();
    
    foreach ($urls as $url) {
        // Custom validation logic
        if (is_valid_audio_url($url)) {
            $validated_urls[] = $url;
        }
    }
    
    return $validated_urls;
}, 10, 2);

Track Audio Saves

// Log when audio data is saved
add_action('wcap_pro_after_save_audio', function($product_id, $audio_data) {
    error_log("Audio saved for product {$product_id}: " . count($audio_data['wcap_audio_urls']) . " files");
    
    // Send to analytics, update counters, etc.
    track_audio_upload($product_id, $audio_data);
});

Frontend Hooks Examples

Customize Player Display

// Modify player configuration before rendering
add_filter('wcap_pro_player_config', function($config, $product_id) {
    $product = wc_get_product($product_id);
    
    // Different settings for different product types
    if ($product->is_type('subscription')) {
        $config['show_waveform'] = true;
        $config['show_time'] = true;
    }
    
    return $config;
}, 10, 2);

Add Custom Player Content

// Add content before audio player
add_action('wcap_pro_before_audio_player', function($product_id, $audio_data) {
    echo '<div class="audio-preview-intro">Listen to samples:</div>';
});

// Add content after audio player
add_action('wcap_pro_after_audio_player', function($product_id, $audio_data) {
    $product = wc_get_product($product_id);
    echo '<p class="audio-preview-note">Full tracks available after purchase of ' . $product->get_name() . '</p>';
});

Custom Audio File Processing

// Process audio file URLs before playing
add_filter('wcap_pro_audio_file_url', function($url, $product_id, $index) {
    // Add authentication tokens, CDN parameters, etc.
    if (strpos($url, 'private-cdn.com') !== false) {
        $url .= '?token=' . generate_access_token($product_id);
    }
    
    return $url;
}, 10, 3);

Track Audio Plays

// Track when audio starts playing
add_action('wcap_pro_audio_play_start', function($product_id, $audio_index) {
    // Send to Google Analytics, custom tracking, etc.
    track_audio_event('play_start', $product_id, $audio_index);
    
    // Update play counts
    $play_count = get_post_meta($product_id, '_audio_play_count', true) ?: 0;
    update_post_meta($product_id, '_audio_play_count', $play_count + 1);
});

// Track when audio stops
add_action('wcap_pro_audio_play_stop', function($product_id, $audio_index) {
    track_audio_event('play_stop', $product_id, $audio_index);
});

Vendor Hooks Examples

Customize Vendor Upload Limits

// Set different upload limits per vendor
add_filter('wcap_pro_vendor_upload_limit', function($limit, $product_id) {
    $vendor_id = get_current_user_id();
    $vendor_subscription = get_user_meta($vendor_id, 'subscription_level', true);
    
    switch ($vendor_subscription) {
        case 'premium':
            return 10;
        case 'pro':
            return 5;
        default:
            return 3;
    }
}, 10, 2);

Add Vendor-Specific Content

// Add instructions for vendors
add_action('wcap_pro_vendor_before_audio_fields', function($product_id, $context) {
    $vendor_id = get_current_user_id();
    $remaining_uploads = get_remaining_uploads($vendor_id);
    
    echo "<div class='vendor-notice'>You have {$remaining_uploads} uploads remaining this month.</div>";
});

Process Vendor Audio Data

// Add vendor branding to audio names
add_filter('wcap_pro_vendor_save_names', function($names, $product_id) {
    $vendor_id = get_current_user_id();
    $vendor_name = get_vendor_name($vendor_id);
    
    foreach ($names as $index => $name) {
        $names[$index] = $name . ' - by ' . $vendor_name;
    }
    
    return $names;
}, 10, 2);

Vendor Analytics

// Track vendor audio uploads
add_action('wcap_pro_vendor_after_save_audio', function($product_id, $audio_data) {
    $vendor_id = get_current_user_id();
    $upload_count = count($audio_data['wcap_audio_urls']);
    
    // Update vendor stats
    update_vendor_upload_stats($vendor_id, $upload_count);
    
    // Send notification
    wp_mail(
        get_vendor_email($vendor_id),
        'Audio Preview Uploaded',
        "Your audio previews for product #{$product_id} have been saved successfully."
    );
});

Settings Hooks Examples

Add Custom Settings Tabs

// Add content to settings pages
add_action('wcap_pro_admin_before_settings', function($tab) {
    if ($tab === 'general') {
        echo '<div class="custom-settings-notice">Custom configuration notice</div>';
    }
});

add_action('wcap_pro_admin_after_settings', function($tab) {
    if ($tab === 'advanced') {
        include 'custom-advanced-settings.php';
    }
});

Modify Settings Template

// Use custom settings template
add_filter('wcap_pro_settings_template_path', function($path, $tab) {
    if ($tab === 'custom') {
        return get_template_directory() . '/woocommerce/audio-preview-custom-settings.php';
    }
    return $path;
}, 10, 2);

Display Control Examples

Conditional Player Display

// Only show audio player for certain user roles
add_filter('wcap_pro_show_audio_player', function($show, $product_id) {
    if (!current_user_can('subscriber')) {
        return false; // Hide from non-subscribers
    }
    
    // Hide from certain product categories
    $product = wc_get_product($product_id);
    $categories = $product->get_category_ids();
    
    if (in_array(25, $categories)) { // Category ID 25
        return false;
    }
    
    return $show;
}, 10, 2);

Dynamic Preview Duration

// Set preview duration based on product price
add_filter('wcap_pro_preview_duration', function($duration, $product_id, $index) {
    $product = wc_get_product($product_id);
    $price = $product->get_price();
    
    if ($price > 100) {
        return 60; // 60 seconds for expensive products
    } elseif ($price > 50) {
        return 45; // 45 seconds for medium-priced
    } else {
        return 30; // 30 seconds for cheaper products
    }
}, 10, 3);

Custom Player Themes

// Add custom player theme based on product category
add_filter('wcap_pro_player_theme', function($theme, $product_id) {
    $product = wc_get_product($product_id);
    $categories = $product->get_category_ids();
    
    if (in_array(15, $categories)) { // Music category
        return 'dark';
    } elseif (in_array(16, $categories)) { // Podcast category
        return 'minimal';
    }
    
    return $theme;
}, 10, 2);

Advanced Integration Examples

WooCommerce Integration

// Add audio preview info to order items
add_action('woocommerce_checkout_create_order_line_item', function($item, $cart_item_key, $values, $order) {
    $product_id = $item->get_product_id();
    $audio_data = get_post_meta($product_id, 'wcap_audio', true);
    
    if (!empty($audio_data['wcap_audio_urls'])) {
        $item->add_meta_data('_has_audio_preview', 'yes');
        $item->add_meta_data('_audio_preview_count', count($audio_data['wcap_audio_urls']));
    }
}, 10, 4);

Email Integration

// Add audio preview links to order emails
add_action('woocommerce_email_order_details', function($order, $sent_to_admin, $plain_text, $email) {
    if ($email->id === 'customer_completed_order') {
        foreach ($order->get_items() as $item) {
            $product_id = $item->get_product_id();
            $audio_data = get_post_meta($product_id, 'wcap_audio', true);
            
            if (!empty($audio_data['wcap_audio_urls'])) {
                echo '<p>This product includes audio previews: <a href="' . get_permalink($product_id) . '">Listen Now</a></p>';
            }
        }
    }
}, 10, 4);

REST API Integration

// Add audio data to WooCommerce REST API
add_filter('woocommerce_rest_prepare_product_object', function($response, $object, $request) {
    $audio_data = get_post_meta($object->get_id(), 'wcap_audio', true);
    
    if (!empty($audio_data)) {
        $response->data['audio_previews'] = array(
            'urls' => $audio_data['wcap_audio_urls'] ?? array(),
            'names' => $audio_data['wcap_audio_names'] ?? array(),
            'theme' => $audio_data['wcap_audio_theme'] ?? 'classic'
        );
    }
    
    return $response;
}, 10, 3);

Utility Functions

Helper Functions for Custom Development

// Get audio data for a product
function get_product_audio_data($product_id) {
    return apply_filters('wcap_pro_get_audio_data', get_post_meta($product_id, 'wcap_audio', true), $product_id);
}

// Check if product has audio previews
function product_has_audio_previews($product_id) {
    $audio_data = get_product_audio_data($product_id);
    return !empty($audio_data['wcap_audio_urls']);
}

// Get audio preview count
function get_audio_preview_count($product_id) {
    $audio_data = get_product_audio_data($product_id);
    return count($audio_data['wcap_audio_urls'] ?? array());
}

// Custom validation function
function is_valid_audio_url($url) {
    $allowed_extensions = apply_filters('wcap_pro_allowed_extensions', array('mp3', 'wav', 'ogg', 'm4a'));
    $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
    return in_array(strtolower($extension), $allowed_extensions);
}
Last updated: November 10, 2025