Skip to main content

Clean up captured source links on import

Free Intermediate

Both core import paths — the setup wizard’s one-time import and Feed Sync — store each RSS <item><link> (the episode’s page on the old host) as _benecaster_source_link, validated by EpisodeMeta::sanitize_source_link() (http/https with a host, at most 2048 characters; anything else leaves the key unset). Many hosts append campaign parameters to that link. Because benecaster_episode_imported fires from both import paths after the meta is written, one callback cleans up both — the wizard import runs no filters at all, so there’s no earlier point to hook.

Nothing in core fetches that page: it’s stored for reference, and read back with EpisodeMeta::get_source_link(), as read-only source_link on GET /episodes/{id}, in the site data export, and through the core wp/v2 meta registration. To change the value before the draft exists — for Feed Sync only — set source_link on benecaster_feed_sync_import_data instead; it’s revalidated after your callback runs.

Code

<?php
add_action(
    'benecaster_episode_imported',
    function ( int $episode_id, int $show_id, SimpleXMLElement $rss_item ): void {
        $meta = new \Benecaster\Episode\EpisodeMeta( $episode_id );
        $link = $meta->get_source_link();

        if ( '' === $link ) {
            return; // No usable <link> on the item.
        }

        $parts = wp_parse_url( $link );
        parse_str( $parts['query'] ?? '', $query );

        foreach ( [ 'utm_source', 'utm_medium', 'utm_campaign', 'ref' ] as $drop ) {
            unset( $query[ $drop ] );
        }

        // set_source_link() revalidates and, on a bad value, removes the key
        // rather than storing ''.
        $meta->set_source_link(
            $parts['scheme'] . '://' . $parts['host'] . ( $parts['path'] ?? '' )
                . ( [] === $query ? '' : '?' . http_build_query( $query ) )
        );
    },
    10,
    3
);

View on GitHub →

Hooks Used

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