Skip to main content

Check episode access in a shortcode or widget

Free Beginner

Use the public access-check functions to conditionally render premium content in any PHP context. benecaster_user_can_access_episode() returns true for public episodes regardless of subscription status. benecaster_user_has_active_membership() asks the show’s membership system whether this person holds any active membership there, free or paid — true separates “wrong plan” from “not a member”. Test for true exactly: it returns null when it cannot tell (the show’s membership plugin is inactive, or none is configured), and an upgrade prompt presumes a membership nobody could confirm, so null gets the subscribe form. Do not use benecaster_get_user_tier_for_show() for this split: it reads the feed token, which a follower, a listener who only bought something outright, and an imported listener who has not paid yet all hold — each would be told to upgrade a subscription they do not have.

Code

<?php
// In a shortcode callback or template:
$episode_id = get_the_ID();
$show_id    = (int) get_post_meta( $episode_id, '_benecaster_show_id', true );

if ( benecaster_user_can_access_episode( $episode_id, $show_id ) ) {
    // May play it — a public episode, or one this person's plan or purchase opens.
    echo '<audio src="' . esc_url( get_post_meta( $episode_id, '_benecaster_audio_url', true ) ) . '" controls></audio>';
} elseif ( true === benecaster_user_has_active_membership( $show_id ) ) {
    // A member whose plan does not include this episode — show an upgrade prompt.
    echo '<p>' . esc_html__( 'Upgrade your subscription to access this episode.', 'my-addon' ) . '</p>';
} else {
    // Not a member (false), or membership could not be confirmed (null) — show the subscribe form.
    echo do_shortcode( '[benecaster_subscribe show_id="' . $show_id . '"]' );
}

View on GitHub →

Need this built rather than just documented? See our services →