Audio Preview Pro – Developer Guide

Get Started

Plugin Architecture

Core Structure

woo-audio-preview-pro/
├── admin/                 # Admin functionality
├── public/               # Frontend functionality
├── includes/             # Core classes
├── assets/               # Shared CSS/JS
├── dokan/               # Dokan integration
├── wcfm/                # WCFM integration
├── wc-vendors/          # WC Vendors integration
└── docs/                # Documentation

Main Classes

  • Woo_Audio_Preview_Pro – Main plugin class
  • Woo_Audio_Preview_Pro_Admin – Admin functionality
  • Woo_Audio_Preview_Pro_Public – Frontend functionality
  • WCAP_Multivendor_Template – Multi-vendor integration

Hooks & Filters

Action Hooks

Core Actions

// Before audio player is rendered
do_action('wcap_pro_before_audio_player', $product_id, $audio_data);

// After audio player is rendered
do_action('wcap_pro_after_audio_player', $product_id, $audio_data);

// Before saving audio data
do_action('wcap_pro_before_save_audio', $product_id, $audio_data);

// After saving audio data
do_action('wcap_pro_after_save_audio', $product_id, $audio_data);

// When audio starts playing
do_action('wcap_pro_audio_play_start', $product_id, $audio_index);

// When audio stops playing
do_action('wcap_pro_audio_play_stop', $product_id, $audio_index);

Admin Actions

// Before admin meta box content
do_action('wcap_pro_admin_before_meta_box', $post);

// After admin meta box content
do_action('wcap_pro_admin_after_meta_box', $post);

// Before settings page content
do_action('wcap_pro_admin_before_settings', $tab);

// After settings page content
do_action('wcap_pro_admin_after_settings', $tab);

Vendor Actions

// Before vendor audio fields
do_action('wcap_pro_vendor_before_audio_fields', $product_id);

// After vendor audio fields
do_action('wcap_pro_vendor_after_audio_fields', $product_id);

// When vendor saves audio data
do_action('wcap_pro_vendor_save_audio', $product_id, $audio_data);

Filter Hooks

Audio Data Filters

// Filter audio player HTML output
$html = apply_filters('wcap_pro_audio_player_html', $html, $product_id, $audio_data);

// Filter audio file URL before processing
$url = apply_filters('wcap_pro_audio_file_url', $url, $product_id, $index);

// Filter player configuration
$config = apply_filters('wcap_pro_player_config', $config, $product_id);

// Filter audio preview duration
$duration = apply_filters('wcap_pro_preview_duration', $duration, $product_id, $index);

// Filter watermark settings
$watermark = apply_filters('wcap_pro_watermark_settings', $watermark, $product_id);

Display Filters

// Filter whether to show audio player for product
$show = apply_filters('wcap_pro_show_audio_player', $show, $product_id);

// Filter player position on product page
$position = apply_filters('wcap_pro_player_position', $position, $product_id);

// Filter player theme
$theme = apply_filters('wcap_pro_player_theme', $theme, $product_id);

// Filter archive display mode
$mode = apply_filters('wcap_pro_archive_display_mode', $mode, $product_id);

Validation Filters

// Filter allowed audio file types
$types = apply_filters('wcap_pro_allowed_file_types', $types);

// Filter maximum file size
$max_size = apply_filters('wcap_pro_max_file_size', $max_size, $product_id);

// Filter CDN domains
$domains = apply_filters('wcap_pro_cdn_domains', $domains);

// Filter upload validation
$valid = apply_filters('wcap_pro_validate_upload', $valid, $file, $product_id);

Vendor Filters

// Filter vendor upload limit
$limit = apply_filters('wcap_pro_vendor_upload_limit', $limit, $vendor_id);

// Filter vendor capabilities
$can_manage = apply_filters('wcap_pro_vendor_can_manage', $can_manage, $vendor_id);

// Filter vendor audio data before save
$data = apply_filters('wcap_pro_vendor_audio_data', $data, $product_id, $vendor_id);

Template Customization

Frontend Templates

Templates can be overridden in your theme:

your-theme/
└── woo-audio-preview-pro/
    ├── audio-player.php
    ├── audio-button.php
    └── archive-player.php

Basic Player Template

// your-theme/woo-audio-preview-pro/audio-player.php
<?php
if (!defined('ABSPATH')) exit;

$audio_data = $args['audio_data'];
$product_id = $args['product_id'];
?>

<div class="custom-audio-player" data-product="<?php echo esc_attr($product_id); ?>">
    <?php foreach ($audio_data['wcap_audio_urls'] as $index => $url): ?>
        <div class="audio-item">
            <span class="audio-name"><?php echo esc_html($audio_data['wcap_audio_names'][$index]); ?></span>
            <audio controls>
                <source src="<?php echo esc_url($url); ?>" type="audio/mpeg">
            </audio>
        </div>
    <?php endforeach; ?>
</div>

Admin Templates

Admin templates can also be customized:

// Filter admin template path
add_filter('wcap_pro_admin_template_path', function($path, $template) {
    if ($template === 'meta-box') {
        return get_template_directory() . '/admin/audio-meta-box.php';
    }
    return $path;
}, 10, 2);

JavaScript API

Frontend JavaScript Events

// Player events
$(document).on('wcap:player:ready', function(e, playerId, config) {
    // Player is ready, playerId and config are available
});

$(document).on('wcap:audio:play', function(e, playerId, audioIndex) {
    // Audio started playing
    // playerId: the player instance ID
    // audioIndex: index of the audio file being played
});

$(document).on('wcap:audio:pause', function(e, playerId, audioIndex) {
    // Audio paused
});

$(document).on('wcap:audio:ended', function(e, playerId, audioIndex) {
    // Audio playback ended
});

Player Control Methods

// Get player instance
var player = window.WCAPPlayer.getInstance(playerId);

// Control playback
player.play();
player.pause();
player.stop();
player.setVolume(0.5);
player.seek(30); // Seek to 30 seconds

// Get player state
var isPlaying = player.isPlaying();
var currentTime = player.getCurrentTime();
var duration = player.getDuration();

Custom Player Integration

// Register custom player
window.WCAPPlayer.registerPlayer('custom', {
    init: function(element, config) {
        // Initialize custom player
    },
    
    play: function() {
        // Play audio
    },
    
    pause: function() {
        // Pause audio
    }
});

Database Schema

Audio Data Structure

Audio data is stored in the wp_postmeta table with meta_key wcap_audio:

$audio_data = array(
    'wcap_audio_urls' => array(
        'https://example.com/audio1.mp3',
        'https://example.com/audio2.mp3'
    ),
    'wcap_audio_names' => array(
        'Track 1 - Introduction',
        'Track 2 - Demo'
    ),
    'wcap_audio_durations' => array(30, 45),
    'wcap_audio_theme' => 'classic'
);

Settings Storage

Plugin settings are stored in the wp_options table:

  • wcap_pro_admin_general_option – General settings
  • wcap_pro_player_settings – Player configuration
  • wcap_pro_watermark_settings – Watermark options
  • wcap_pro_advanced_settings – Advanced options

Extending Functionality

Custom Audio Sources

// Add custom audio source handler
add_filter('wcap_pro_audio_file_url', function($url, $product_id, $index) {
    if (strpos($url, 'custom://') === 0) {
        // Handle custom protocol
        $file_id = str_replace('custom://', '', $url);
        return your_custom_url_resolver($file_id);
    }
    return $url;
}, 10, 3);

Custom Player Themes

// Register custom theme
add_filter('wcap_pro_player_themes', function($themes) {
    $themes['mytheme'] = 'My Custom Theme';
    return $themes;
});

// Enqueue theme styles
add_filter('wcap_pro_player_theme', function($theme, $product_id) {
    if ($theme === 'mytheme') {
        wp_enqueue_style('my-audio-theme', get_template_directory_uri() . '/css/audio-theme.css');
    }
    return $theme;
}, 10, 2);

Custom Validation Rules

// Add custom file validation
add_filter('wcap_pro_validate_upload', function($valid, $file, $product_id) {
    // Custom validation logic
    if (!your_custom_validation($file)) {
        return new WP_Error('invalid_file', 'Custom validation failed');
    }
    return $valid;
}, 10, 3);

Custom Watermarks

// Custom watermark processor
add_filter('wcap_pro_watermark_settings', function($watermark, $product_id) {
    if ($watermark['watermark_type'] === 'custom') {
        $watermark['processor'] = 'your_custom_processor';
    }
    return $watermark;
}, 10, 2);

function your_custom_processor($audio_url, $watermark_settings) {
    // Process audio with custom watermark
    return $processed_url;
}

Vendor Integration

// Custom vendor platform support
add_filter('wcap_pro_vendor_platforms', function($platforms) {
    $platforms['myvendor'] = array(
        'name' => 'My Vendor Platform',
        'class' => 'MyVendor_Audio_Integration'
    );
    return $platforms;
});

class MyVendor_Audio_Integration {
    public function render_fields($product_id) {
        // Render vendor-specific fields
    }
    
    public function save_data($product_id, $data) {
        // Save vendor data
    }
}

Code Examples

Custom Player Button

// Add custom play button to product thumbnails
add_action('woocommerce_product_thumbnails', function() {
    global $product;
    $audio_data = get_post_meta($product->get_id(), 'wcap_audio', true);
    
    if (!empty($audio_data['wcap_audio_urls'])) {
        echo '<button class="custom-play-btn" data-product="' . $product->get_id() . '">▶ Preview</button>';
    }
});

Analytics Integration

// Track audio plays
add_action('wcap_pro_audio_play_start', function($product_id, $audio_index) {
    // Log to analytics
    your_analytics_track('audio_play', array(
        'product_id' => $product_id,
        'audio_index' => $audio_index
    ));
});

Dynamic Duration Control

// Set dynamic preview duration based on product category
add_filter('wcap_pro_preview_duration', function($duration, $product_id, $index) {
    $product = wc_get_product($product_id);
    
    if ($product->is_type('variable')) {
        return 60; // Longer preview for variable products
    }
    
    return $duration;
}, 10, 3);

Custom CSS Injection

// Add custom CSS based on product type
add_action('wp_head', function() {
    if (is_product()) {
        global $product;
        $audio_data = get_post_meta($product->get_id(), 'wcap_audio', true);
        
        if (!empty($audio_data)) {
            echo '<style>.wcap-player { /* custom styles */ }</style>';
        }
    }
});

Best Practices

Performance

  • Use lazy loading for audio files
  • Implement proper caching strategies
  • Optimize audio file sizes
  • Use CDN for better delivery

Security

  • Always validate and sanitize input
  • Use nonces for form submissions
  • Implement proper capability checks
  • Sanitize file uploads

Compatibility

  • Test with popular themes
  • Check WordPress version compatibility
  • Verify WooCommerce version support
  • Test multi-vendor integrations

Debugging

// Enable debug mode
define('WCAP_PRO_DEBUG', true);

// Debug logging
do_action('wcap_pro_log', 'Debug message', $data);
Last updated: November 10, 2025