Skip to main content

benecaster_download_redirected

Action Free Since v1.0.0

Fires just before the 302 redirect to the audio host is issued, on every proxied download request. This hook fires unconditionally — it runs whether or not the benecaster_download_log_entry filter skipped persistence.

This hook fires synchronously before the redirect response headers are sent. Callbacks block the response, so use async dispatch (scheduled events, queue jobs) for any work involving external HTTP calls or database-heavy operations. The audio file URL is not passed to this hook — retrieve it from episode post meta using $episode_id if needed.

Parameters

Name Type Default Description
$episode_id int ID of the episode being downloaded
$show_id int ID of the show
$user_id int WordPress user ID resolved from the token; 0 for public-tier proxied downloads

Examples

Dispatch async webhook on each download

add_action(
    'benecaster_download_redirected',
    function ( int $episode_id, int $show_id, int $user_id ): void {
        wp_schedule_single_event(
            time(),
            'my_plugin_send_download_webhook',
            [ [ 'episode_id' => $episode_id, 'show_id' => $show_id, 'user_id' => $user_id, 'timestamp' => gmdate( 'c' ) ] ]
        );
    },
    10, 3
);

add_action(
    'my_plugin_send_download_webhook',
    function ( array $payload ): void {
        wp_remote_post(
            'https://hooks.example.com/download-event',
            [ 'body' => wp_json_encode( $payload ), 'headers' => [ 'Content-Type' => 'application/json' ], 'timeout' => 5, 'blocking' => false ]
        );
    }
);

Increment a real-time counter

add_action(
    'benecaster_download_redirected',
    function ( int $episode_id, int $show_id, int $user_id ): void {
        $cache_key = "show_downloads_today_{$show_id}_" . gmdate( 'Y-m-d' );
        if ( false === wp_cache_get( $cache_key ) ) {
            wp_cache_set( $cache_key, 0, '', DAY_IN_SECONDS );
        }
        wp_cache_incr( $cache_key );
    },
    10, 3
);

Notes

A $user_id of 0 indicates a public feed download authenticated by token but with no associated WordPress user. From within a callback you can call wp_die() to abort the download entirely — the subscriber will see a WordPress error page. Reserve this for extraordinary circumstances such as a compliance hold.