Skip to main content

Send your own design for one Benecaster email, per show

Free Intermediate

For an add-on that lets a podcaster write their own version of an email — the welcome, a receipt — without a theme file. Return the stored HTML from benecaster_email_template_body with {{tag}} placeholders left in, and Benecaster fills them in, escaped, and wraps the result in the usual header and footer. Gate it on benecaster_addon_is_active() so the design applies only where your add-on is switched on.

Three things are easy to miss. Return placeholders, never values: a value you substitute yourself is not escaped. Stored content beats a theme override, so return it only for a show that has one. And an email with no show — address verification, donor-data erasure — arrives with $show_id null, so check benecaster_addon_is_active_for_any_show() instead.

A merge tag your add-on adds must be declared in benecaster_email_merge_tag_catalog too, or an editor cannot offer it — and say if it carries HTML, or it arrives in a stored body as escaped text.

Code

<?php
add_filter( 'benecaster_email_template_body', static function ( ?string $html, string $type, array $tags, ?int $show_id ): ?string {
    if ( 'welcome' !== $type ) {
        return $html;
    }
    // No show (address verification, donor-data erasure): gate on the add-on being on anywhere.
    $active = null === $show_id
        ? benecaster_addon_is_active_for_any_show( 'my-addon' )
        : benecaster_addon_is_active( 'my-addon', $show_id );
    if ( ! $active ) {
        return $html;
    }
    $stored = null === $show_id ? '' : (string) get_post_meta( $show_id, '_my_addon_welcome_html', true );

    // '' means "nothing stored": keep Benecaster's template (or the theme's override).
    return '' !== $stored ? $stored : $html;
}, 10, 4 );

// A tag your add-on adds must be declared too, or an editor cannot offer it —
// and say if it carries HTML, or it arrives in a stored body as escaped text.
add_filter( 'benecaster_email_merge_tag_catalog', static function ( array $catalog ): array {
    $catalog['host_signature'] = [
        'label'       => __( 'Host signature', 'my-addon' ),
        'description' => __( 'The host’s sign-off, with formatting.', 'my-addon' ),
        'types'       => [ 'welcome' ],
        'sample'      => '<em>— Sam</em>',
        'kind'        => 'html',
    ];
    return $catalog;
} );

View on GitHub →

Hooks Used

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