Skip to main content

Decide something on the website from what the visitor holds

Premium Intermediate

Your add-on shows or hides something on an episode page, in a wp/v2 response or in a check function of its own, by membership tier and by buy-up. Read the visitor’s holdings once with benecaster_get_user_holdings() and test each part. Don’t read the tier saved on their feed token, which misses a second level and lags a Tier Mapping change, and don’t rebuild the rules from the database.

  • Test each part separately. A buy-up’s slug or a purchase’s slug is not a tier; passed to [benecaster_tier_gate] or compared as one, it matches nothing, and the buyers are the ones locked out.
  • This is the website’s answer, not the feed’s. On a show past its licence, or for a listener over the plan’s limit, it still answers as before, as the episode page does. In a feed callback, read the listener_slugs in the callback’s context.
  • public is never in it: test for public content separately. A signed-out visitor gets three empty lists.
  • With Preview as Tier on, the podcaster sees exactly what a member on the previewed tier sees.
  • Call it once per request. It is not cached, and it asks the membership plugin each time.
  • Pass a user ID as the second argument to ask about someone other than the current visitor.

Code

<?php
add_filter( 'the_content', static function ( string $content ): string {
    if ( ! is_singular( 'benecaster_episode' ) || ! function_exists( 'benecaster_get_user_holdings' ) ) {
        return $content;
    }

    $show_id  = (int) get_post_field( 'post_parent', get_the_ID() );
    $holdings = benecaster_get_user_holdings( $show_id ); // the current visitor
    $bonus    = benecaster_get_buyup_slug( (int) get_option( 'my_addon_bonus_buyup_id' ), $show_id );

    $allowed = [] !== array_intersect( [ 'gold', 'platinum' ], $holdings['tiers'] )
        || ( null !== $bonus && in_array( $bonus, $holdings['buyups'], true ) );

    return $allowed ? $content . my_addon_render_bonus( get_the_ID() ) : $content;
} );

View on GitHub →

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