Skip Navigation

[Resolved] Does ‘cred_notification_recipients’ apply to scheduled notifications

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

Problem:
Where and how does the filter

cred_notification_recipients

apply?

Solution:
This filter allows you to add or modify email recipients of CRED notifications, optionally updating to, cc, and bcc recipients. It can apply to all notifications or target individual notifications.
So, if you do not target ONE specific notification, it will apply to all of them.
And always, it applies every time the notification(s) is used.

If you exit, the obvious PHP behaviour is... to abort.
Means no code will be applied in those cases.

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

This support ticket is created 6 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by tinaH 6 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#569262

I have a question regarding CRED Hook 'cred_notification_recipients'.

The form has 4 notifications, first is on submit and the others on 10, 5 and 2 days before expiration date.

Will my function below affect all scheduled notifications or only first notifications, on submit?
Will it be executed before every scheduled notification or does it only run once, when the form is submitted?
If any of the emails I get with the function are changed, after first submit, will future notifications use the "updated" email or not?

All notifications are set to email "Author".
In my function I exit the function if I fail to get an email address, does this mean that the notifications will be sent to post Author?
Does my function replace the original form setting, to email Author, or will it email Author AND the email returned by the function?

I apologize for all these question but your documentation doesn't cover these questions.

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)) {
            //mail to author fallback?
            exit;
        } else {
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => $email,
                'name'      =>  '',
                'lastname'  =>  ''
            );
            return $recipients;
        }
    }
}
add_filter('cred_notification_recipients', 'b_invoice_mail', 10, 4);
#569428

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

This will apply to:

This filter allows you to add or modify email recipients of CRED notifications, optionally updating to, cc, and bcc recipients. It can apply to all notifications or target individual notifications.

So, if you do not target ONE specific notification, it will apply to all of them.
And always, it applies every time the notification(s) is used.

If you exit, the obvious PHP behaviour is... to abort.
Means no code will be applied in those cases.

I hope all is clear now.

#569551

Almost (feel a bit thick ... 😉
Don't know the answer to my last question:
Does my function replace the original form setting, to email Author, or will it email Author AND the email returned by the function?

#569767

The hook required a return $recipients at the end.
Just return; wasn't enough.

Final working function if anyone reads this:

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);
#569769

Just this final question:
Does my function replace the original form setting, to email Author, or will it email Author AND the email returned by the function?

#570046

You can do both.
When you use as example "bcc" it should use both and when you use "to*, it should replace it should replace it.

#570125

:):):)

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.