Skip to main content

Gate anything on a listener’s episode purchases

Free Beginner

The two published accessors for non-tier access — use them anywhere you would use benecaster_user_has_buyup() for a buy-up or benecaster_get_user_tier_for_show() for a tier, to gate a template part, a page, or a download link on what a listener has bought outright.

Both take the show first and default $user_id to the current user, matching benecaster_get_user_tier_for_show(). A grant cannot exist without a show, so $show_id is required rather than optional.

This is the third recipe in the set: Grant a listener access to an episode or a bundle from an add-on records the grant in Benecaster’s own table, and Sell episode access from your own add-on answers the same question from an add-on’s own records instead. Choosing between those two is the first decision an add-on developer makes; this recipe is the one both of them lead to — reading the answer back out to gate something.

Both functions return what the feed serves, not the raw rows Benecaster stores. The answer resolves through Entitlement\GrantSlugResolver, so a grant registered through benecaster_entitlement_grant_slugs is included. Reading benecaster_entitlements directly instead would answer “nothing” on a site whose add-on supplies its own grants, while the listener’s feed carried those episodes.

Buy-ups are not included, and that is deliberate. A buy-up is a recurring subscription item that dies with the membership it rides on; a grant is owned outright and survives an over-limit demotion, a license grace period and the end of the membership. Use benecaster_user_has_buyup() for that question instead.

A grant slug is not a tier slug. Feeding one to a tier-shaped API ([benecaster_tier_gate]‘s min_tier, or benecaster_get_user_tier_for_show()) fails closed for exactly the people who bought it, silently — the same trap buy-ups carry.

Only live grants are returned: expires_at in the future, or NULL for a lifetime grant. An expired grant is absent but its row survives, so an add-on can still see the person once held it.

Code

<?php
// One named product or bundle — the direct form, and the one to use in a gate.
if ( benecaster_user_holds_grant( get_the_ID(), 'season-one' ) ) {
    get_template_part( 'parts/season-one-extras' );
}

// Everything this listener owns on the show — live grants only.
foreach ( benecaster_get_user_grants( $show_id, $user_id ) as $slug ) {
    echo esc_html( $slug );   // your own product names live in your add-on
}

View on GitHub →

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