Skip to main content

EmailRenderer

\Benecaster\Email\EmailRenderer

Class Free

Turns an email template into the HTML and plain-text message Benecaster sends. It finds the template file, renders it with the email’s merge tags, and wraps the result in the base template that carries the header, footer and business address.

The one method an add-on calls is add_template_dir(), to register a directory of its own email templates. Everything else on this class runs inside the send pipeline: to send an email, call benecaster_send_email() and let it render for you.

Templates are looked up in this order, every locale-specific location before any unlocalised one: child theme, parent theme, Benecaster’s templates/emails/, then each registered add-on directory in registration order. The first file that exists wins, so a theme can override an add-on’s template, and an add-on cannot replace one of Benecaster’s.

Methods

Method Visibility Since Description
add_template_dir( string $dir ): void Public — Registers a directory an add-on ships email templates in. The directory is the template root ({dir}/{template}.php), with translations in {dir}/{locale}/{template}.php. Call it on benecaster_boot against the instance from the container, which is the one Benecaster sends with. No filesystem check is made, so a directory that does not exist never matches. Registering the same directory twice has no effect, and a trailing slash is ignored.

Hooks Fired

Notes

Name your templates after your add-on. Benecaster's own templates are found before a registered directory, so an add-on file named welcome.php or base.php is never loaded, and nothing reports it.

This is not benecaster_register_template_directory(). That function registers front-end templates, which have their own loader; registering a directory with one does not register it with the other. An add-on that ships both front-end templates and emails calls both.

A template that cannot be found costs more than its body. When no file matches, render() returns empty HTML and text without loading base, so the message also loses its branded header and footer and the business address. Supplying a body through benecaster_email_body_html_{$type} does not replace a template for that reason.