Skip to main content

Add a custom field to the RSS feed

Free Intermediate Since v1.0.0

Two hooks work in combination here: benecaster_episode_custom_fields exposes a custom field’s value through the episode data layer, and benecaster_rss_extra_item_tags appends the corresponding XML to each RSS item. Use this pattern when you need a non-standard namespace element (e.g. a sponsor tag, a transcript URL, or a custom CMS field) in the feed.

Code

<?php
add_filter(
    'benecaster_rss_extra_item_tags',
    function ( array $tags, int $episode_id, int $show_id ): array {
        $sponsor = benecaster_get_field( 'sponsors', $episode_id );
        if ( is_string( $sponsor ) && '' !== $sponsor ) {
            $tags['myshow:sponsor'] = $sponsor;
        }
        $count = (int) get_post_meta( $episode_id, '_my_addon_chapter_count', true );
        if ( $count > 0 ) {
            $tags['myshow:chapterCount'] = [
                'value'      => (string) $count,
                'attributes' => [ 'unit' => 'chapters' ],
            ];
        }
        return $tags;
    },
    10,
    3
);

View on GitHub →

Hooks Used