Skip to main content

Capture custom RSS namespace data during an import

Free Beginner

benecaster_episode_imported fires after each episode is saved, whether it came from a one-time import or from Feed Sync — both are core. Feed Sync also runs benecaster_feed_sync_import_data first, letting you reshape the raw RSS item data before it’s written. Use the pair to carry custom RSS namespace elements — a sponsor, a series ID, anything your old host wrote into its own namespace — onto the imported episode instead of losing them at the boundary.

benecaster_episode_imported hands you the raw <item> as a SimpleXMLElement, not an array, and there is no raw_xml key anywhere. Type the parameter correctly or PHP will fatal before your callback body runs.

The value goes into a Benecaster custom field with benecaster_update_field(), so it appears in the episode editor and reads back through benecaster_get_field(). The write fires benecaster_episode_field_values_updated exactly as an editor save does, so any add-on listening for “this episode’s fields changed” sees imported values too. The field’s group must be attached to Episodes: a field on another post type writes nothing and raises _doing_it_wrong(), which shows with WP_DEBUG on.

Feed Sync also runs benecaster_feed_sync_import_data before the draft is created, but that filter only reshapes the keys it documents. To store a value with no documented key, use this recipe.

Code

<?php
// The ID of your "Sponsor" field — shown (click to copy) in the ID column
// of Benecaster → Fields → edit the field group. benecaster_update_field()
// addresses fields by ID, like benecaster_get_field().
const MY_ADDON_SPONSOR_FIELD_ID = 12;

add_action(
    'benecaster_episode_imported',
    function ( int $episode_id, int $show_id, SimpleXMLElement $rss_item ): void {
        $ns = $rss_item->getNamespaces( true );

        if ( ! isset( $ns['mystudio'] ) ) {
            return;
        }

        $mystudio = $rss_item->children( $ns['mystudio'] );
        $sponsor  = trim( (string) ( $mystudio->sponsor ?? '' ) );

        if ( '' !== $sponsor ) {
            benecaster_update_field( MY_ADDON_SPONSOR_FIELD_ID, $episode_id, $sponsor );
        }
    },
    10,
    3
);

View on GitHub →

Hooks Used

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