Skip to main content

Replace the built-in unsubscribe URL with a branded opt-out center

Premium Intermediate

By default {{unsubscribe_url}} points to the WordPress site’s unsubscribe endpoint, ?benecaster_unsub={token}. Override it at priority 20 (after Benecaster’s own inject_unsubscribe_url at priority 10) to send listeners to a branded opt-out centre instead.

Keep the signed token. The token is the only thing that says who the listener is, which podcast and which kind of email they are leaving, and nothing can be edited without breaking it. So a custom page can’t build the link from a user and show ID. Pass the token along, as below, and have your page either send the listener on to home_url( '/?benecaster_unsub=' . rawurlencode( $token ) ), Benecaster’s own confirmation page, or read it with UnsubscribeManager::resolve_link() and record the opt-out with opt_out(), so the compliance log still gets its row.

Code

<?php
add_filter( 'benecaster_email_merge_tags', function ( array $tags, string $type, ?int $user_id, ?int $show_id ): array {
    // Only override for broadcast emails — leave every other type on the default link.
    if ( 'broadcast' !== $type || empty( $tags['unsubscribe_url'] ) ) {
        return $tags;
    }

    // Carry Benecaster's signed token to your page. Never build the link from the user and show IDs:
    // the token is what proves who the listener is and what they are leaving.
    parse_str( (string) wp_parse_url( $tags['unsubscribe_url'], PHP_URL_QUERY ), $query );
    if ( ! empty( $query['benecaster_unsub'] ) ) {
        $tags['unsubscribe_url'] = add_query_arg(
            'token',
            rawurlencode( $query['benecaster_unsub'] ),
            'https://example.com/email-preferences/'
        );
    }
    return $tags;
}, 20, 4 ); // priority 20 runs after Benecaster's inject_unsubscribe_url at priority 10

View on GitHub →

Hooks Used

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