Skip to main content

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

Free Intermediate Since v1.0.0

By default {{unsubscribe_url}} points to the WordPress site’s query-string endpoint. Override it to redirect subscribers to a custom opt-out centre. Use priority 20 (runs after core’s inject_unsubscribe_url at priority 10). When building the URL for a custom opt-out page that calls back into WordPress, retrieve the token via UnsubscribeManager::build_unsubscribe_url() and embed it as a parameter so the custom page can POST to the built-in endpoint for compliance logging.

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 transactional emails using the default.
    if ( 'broadcast' !== $type ) {
        return $tags;
    }
    if ( null !== $user_id && null !== $show_id ) {
        $tags['unsubscribe_url'] = add_query_arg( [
            'user'    => $user_id,
            'show'    => $show_id,
            'ref'     => 'benecaster',
        ], 'https://example.com/email-preferences/' );
    }
    return $tags;
}, 20, 4 ); // priority 20 runs after core's inject_unsubscribe_url at priority 10

View on GitHub →

Hooks Used