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 that is served. It runs whether or not the benecaster_download_log_entry filter skipped persistence.

A download refused because the show’s licence is inactive never reaches this hook. The refusal happens before the audio file is resolved and before the download is logged, so on that path this action does not fire, benecaster_download_logged does not fire, and no download log row is written. There is no signal for a refused download at all — counting downloads from either hook cannot tell “nobody tried” apart from “everybody was refused”.

That floor is not zero, which is the easier mistake to make. Followers are exempt from the licence refusal, so their downloads still resolve, still log, and still fire this action. A show whose licence has lapsed sees its counts collapse to follower traffic only — a non-zero count is not evidence the licence is healthy. Read licence state directly rather than inferring it from download volume.

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.