Skip to main content

Register a fully custom XML namespace handler from an add-on

Free Advanced

Podcast apps read far more than the standard RSS fields. Podcasting 2.0, iTunes, and Podping each define their own tags, and new ones keep appearing. An add-on that needs to publish a tag Benecaster does not ship has to put it somewhere — and editing the feed markup directly means fighting the cache and breaking on every release.

benecaster_feed_namespaces is the supported alternative. Append a namespace handler to the array and the feed compiler does the rest: it declares your xmlns on the feed, merges your channel-level tags, and merges your per-episode tags. Nothing in the compiler needs to change.

A handler is responsible for three things — the namespace prefixes and URIs it owns, the tags it contributes for a show, and the tags it contributes for an episode. The example below shows the full shape.

Code

<?php
// In your add-on's boot class:
add_filter( 'benecaster_feed_namespaces', function ( array $handlers ): array {
    $handlers[] = new \MyAddon\MyCustomNamespace();
    return $handlers;
} );

// MyCustomNamespace implements FeedNamespaceInterface:
class MyCustomNamespace implements \Benecaster\Feed\Namespaces\FeedNamespaceInterface {
    public function get_xmlns(): array {
        return [ 'mycustom' => 'https://myaddon.example.com/ns/1.0' ];
    }

    public function channel_tags( int $show_id ): array {
        $value = get_post_meta( $show_id, '_my_addon_channel_tag', true );
        return $value ? [ '<mycustom:channel-tag>' . esc_xml( $value ) . '</mycustom:channel-tag>' . "\n" ] : [];
    }

    public function episode_tags( int $episode_id, int $show_id ): array {
        $value = get_post_meta( $episode_id, '_my_addon_episode_tag', true );
        return $value ? [ '<mycustom:episode-tag>' . esc_xml( $value ) . '</mycustom:episode-tag>' . "\n" ] : [];
    }
}

View on GitHub →

Hooks Used

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