Skip to main content

Annotate episode references with tracked redirect URLs

Free Intermediate

Points each reference at a tracked redirect URL without changing what is stored. The filter runs wherever Benecaster renders references — the episode page (page), each feed item’s <content:encoded> show notes (rss) and benecaster_get_episode_references_html() for an email (email) — and never on the REST reads the episode editor uses, so the editor keeps showing, and saving, the real destination. The fourth argument names the context; the example marks each link with it, so the redirect handler knows where the click came from.

Tracked URLs are keyed by reference_id, the library ID shared by every episode that cites the reference; id is the per-episode usage row and is re-created on every save. The Outlinks add-on uses this pattern for its /go/ links.

Code

<?php
add_filter( 'benecaster_episode_references', function ( array $references, int $episode_id, int $show_id, string $context = 'page' ): array {
    // Runs on the episode page ('page'), in the feed ('rss') and in emails ('email').
    // Keyed by reference_id, which stays the same across every episode that cites the reference.
    $tracked = (array) get_option( 'myplugin_tracked_references', [] );
    foreach ( $references as &$ref ) {
        $reference_id = (int) ( $ref['reference_id'] ?? 0 );
        if ( isset( $tracked[ $reference_id ] ) ) {
            // Mark where the click came from; the redirect handler reads it.
            $ref['url'] = add_query_arg( 'src', $context, $tracked[ $reference_id ] );
        }
    }
    unset( $ref );
    return $references;
}, 10, 4 );

View on GitHub →

Hooks Used

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