Helper function to render a blade template


I would like to create a helper to send emails with wp_mail using a blade template.

The idea would be to create a helper in the setup.php

The problem of doing this is that I don’t have access to the Acorn Application.

I would like to make a helper this way:

function sendCustomerMail(string $to, string $subject) {
    $content = \Roots\view('email.quote')->render();
    wp_mail($to, $subject, $content);

But when I try to do that, I get an error:

Uncaught Error: Call to undefined function app() in xxxx/vendor/roots/acorn/src/Roots/helpers.php:70

Anyone have an idea how to render a blade view if we are not in a Composer or Component?

In order to access to the Root\view method I went through a action/filter like so :

add_action('init', function () {
    add_filter('inrage/mail-content', function ($layout, $args) {
        return view($layout, $args)->render();
    }, 10, 2);

In the init action, Acorn is already loaded and we can access to the Application and its statics methods.

Now i can modify my sendCustomerMail method :

function sendCustomerMail(string $to, string $subject) {
    $content = apply_filters('inrage/mail-content', 'email.quote', [
        'title' => 'My Sweat Title',
    wp_mail($to, $subject, $content);
1 Like