Skip to main content

Notify a tier once when an episode opens to it

Premium Beginner

benecaster_availability_unlocked fires once per episode and tier, when that tier can first hear a published episode: either the episode is published with the tier’s date already passed, or a scheduled date passes on a published episode. It never fires for a draft and never fires twice, so it is the hook to use when you tell people about a new episode.

$available_datetime is the tier’s own date. When an episode is published with a date in the past (a back-dated episode, or an old imported draft published today), it can be long ago, so compare it to the current time if you only want to announce new episodes.

Code

<?php
add_action( 'benecaster_availability_unlocked', function ( int $episode_id, int $show_id, string $tier_slug, int $available_datetime ) {
    // Skip back-dated publishes: anything that "unlocked" more than two days ago is not news.
    if ( $available_datetime < time() - 2 * DAY_IN_SECONDS ) {
        return;
    }
    my_push_service_notify_tier( $show_id, $tier_slug, sprintf(
        /* translators: %s: episode title */
        __( 'New episode for you: %s', 'my-plugin' ),
        get_the_title( $episode_id )
    ) );
}, 10, 4 );

View on GitHub →

Hooks Used

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