Skip to main content

Give the podcaster “Preview” and “Send me a test” for any email

Free Intermediate

benecaster_render_email_preview() and benecaster_send_test_email() both render the email exactly as it would be sent — every merge-tag, subject and body filter, a stored design and a theme override included — and fill empty tags with the catalogue’s sample values. benecaster_get_email_merge_tag_catalog() lists the tags an email offers, for an editor’s autocomplete.

Two things are easy to miss. Never build a preview from EmailRenderer::render(): it skips the merge-tag and body filters, so the preview can differ from the mail. And a test is not a send of that type: opt-outs are skipped, benecaster_after_email_send does not fire, and the email log records it as test.

Code

<?php
add_action( 'rest_api_init', static function (): void {
    register_rest_route( 'my-addon/v1', '/emails/(?P<type>[a-z_]+)/preview', [
        'methods'             => 'GET',
        'permission_callback' => static fn() => current_user_can( 'manage_options' ),
        'callback'            => static function ( \WP_REST_Request $request ) {
            $type    = (string) $request['type'];
            $show_id = absint( $request->get_param( 'show_id' ) ) ?: null;
            return [
                'preview' => benecaster_render_email_preview( $type, [], $show_id ), // subject, html, text
                'tags'    => benecaster_get_email_merge_tag_catalog( $type ),         // for the autocomplete
            ];
        },
    ] );

    register_rest_route( 'my-addon/v1', '/emails/(?P<type>[a-z_]+)/test', [
        'methods'             => 'POST',
        'permission_callback' => static fn() => current_user_can( 'manage_options' ),
        'callback'            => static function ( \WP_REST_Request $request ) {
            $to       = sanitize_email( (string) $request->get_param( 'to' ) ) ?: wp_get_current_user()->user_email;
            $queue_id = benecaster_send_test_email( (string) $request['type'], $to, [], absint( $request->get_param( 'show_id' ) ) ?: null );
            return 0 < $queue_id ? [ 'queued' => true ] : new \WP_Error( 'not_sent', __( 'The test could not be sent.', 'my-addon' ), [ 'status' => 400 ] );
        },
    ] );
} );

View on GitHub →

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