Skip to main content

Annotate episode references with tracked redirect URLs

Free Intermediate

Points each reference on the episode page at a tracked redirect URL without changing what is stored. The filter runs only when the References section renders — never on the REST reads the episode editor uses — so the editor keeps showing, and saving, the real destination. Tracked URLs are keyed by reference_id, the library ID shared by every episode that cites the reference. To attribute clicks to an episode as well, add $episode_id to the URL you return: core appends nothing itself. 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 ): array {
    // Runs only when the episode page renders its References section.
    // 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 ] ) ) {
            $ref['url'] = $tracked[ $reference_id ];
        }
    }
    unset( $ref );
    return $references;
}, 10, 3 );

View on GitHub →

Hooks Used

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