Let visitors who can’t hear an episode play a preview clip
No code needed for the common case. In the episode editor, open Media and paste the clip’s URL into Preview clip URL — a trailer or the first few minutes, hosted wherever the episode is. On the website, anyone who can’t hear the episode then gets a player with the clip where the locked message was: on the episode page, on its archive card, and in [benecaster_player]. Subscribers who can hear it get the full file. The clip is never put in a feed. Benecaster does this with its own callbacks on both filters at priority 10 (Episode\PreviewClipPlayer). The episode needs its main audio URL: one with only an embed or a video keeps its locked message.
This code example is for the advanced case: a clip you don’t store, e.g. one your host cuts on request. Benecaster opens the player only for episodes with a stored clip, so a custom clip source needs both filters. Whatever you open, you must swap — a player opened by benecaster_show_episode_player plays the URL benecaster_episode_audio_url hands it, so opening the player without the swap gives a locked visitor the whole episode. $tier_slug is null when the web player makes the call and a string in a feed or tracked download.
Code
<?php
// Open the player for locked visitors, but only where a clip exists and the
// episode has a main audio URL. Without one, the player template falls back
// to the episode's embed or video, and that would be the full episode.
add_filter( 'benecaster_show_episode_player', function ( bool $show, int $episode_id ): bool {
if ( $show ) {
return true;
}
$audio = (string) get_post_meta( $episode_id, '_benecaster_audio_url', true );
return '' !== $audio && '' !== my_host_preview_url( $episode_id );
}, 20, 2 );
// Swap in the clip for anyone who can't hear the episode, on the web only.
add_filter( 'benecaster_episode_audio_url', function (
string $url,
int $episode_id,
?string $tier_slug = null
): string {
if ( null !== $tier_slug ) {
return $url;
}
$show_id = (int) get_post_field( 'post_parent', $episode_id );
if ( benecaster_user_can_access_episode( $episode_id, $show_id ) ) {
return $url;
}
$clip = my_host_preview_url( $episode_id );
return '' !== $clip ? $clip : $url;
}, 20, 3 );
Hooks Used
Need this built rather than just documented? See our services →