Skip Navigation

[Resolved] Replacing existing email with ‘cred_notification_recipient’

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to replace the To: recipient of a CRED notification email?

Solution:
Client is using the cred_notification_recipients filter to customise the To: recipient of a CRED notification email, but her code is *adding* another To: recipient rather than replacing the existing one.

A minor tweak of her code was required to fix the issue, as outlined below.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by tinaH 6 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#653614

This relates to an older topic I created before with Beda: https://toolset.com/forums/topic/does-cred_notification_recipients-apply-to-scheduled-notifications/

My code works but it ADDS an additional recipient. I want it to replace the existing recipient.
Reading through the conversation my understanding is that the filter is supposed to replace ... not add.

This is what I have:

function b_invoice_mail($recipients, $notification, $form_id, $post_id) {
    if (5803 == $form_id) {
        //applies to all invoice form notifications
        $exhibitor_id = get_post_meta($post_id, '_wpcf_belongs_exhibitor_id', true);
        if (!empty($exhibitor_id)) {
            $email = get_post_meta($exhibitor_id, 'wpcf-email', true);
        } else {
            $email = get_post_meta($post_id, 'wpcf-book-exhib-mail', true);
        }
        if (!empty($email)) {
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => $email,
                'name'      =>  '',
                'lastname'  =>  ''
            );
        }//else use form email settings
    }
    //this hook requires 'return $recipient' here, else disabling notifications for all forms
    //plain return; does not work
    return $recipients;
}
add_filter('cred_notification_recipients', 'b_invoice_mail', 10, 4);
#655316

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

Your code is adding a new 'to' recipient rather than overwriting the existing one.

If you dump $recipients to your debug log you'll see that it looks something like this:

Array
(
    [0] => Array
        (
            [to] => to
            [address] => nigel.a@onthegosystems.com
            [name] => 
            [lastname] => 
        )

)

If you want to replace an existing recipient you should replace $recipients[0] rather than adding another array to it.

#657627

Super