I have a parent/child relationship. In the parent post I have an email field that accepts multiple email addresses.
I would like to use all of those email address for the CRED email notification when a child post is created.
I have tried:
[types field='email-list' separator='; ' output='raw'][/types]
in the Additional Recipients:
I created a field in the child form (thinking that it might be a parent issue with the notifications) and set the value to the parent post field which resulted in the content of that field being "name@address.com; name2@address2.com" etc. But that didn't work either.
Hi, rather than using a shortcode to do this from the notifications GUI, I recommend using the CRED API "cred_notification_recipients" function. This function will allow you to modify the recipients of a notification programmatically, and use CC and BCC effectively. We have an example here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
Click the orange "More" button to see the example.
I could probably manage that if I wanted to hard code the email addresses, but I'd like to use addresses from a parent post.
Well yes, the example only uses a single email address - you must extend this example to fit your needs. The notification recipients API gives you access to everything you need to know to determine the new post's parent, get the parent's custom field values, and loop over them to build your recipients list.
Toolset stores a reference to each parent post in its child post's postmeta using the meta key "_wpcf_belongs_parentslug_id". So if your parent post type has the slug "parentposttype" then you will find a postmeta entry for the child post with the key "_wpcf_belongs_parentposttype_id", and the value will be the parent post's ID. Use that information to query the parent postmeta and fetch all the emails:
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id)
{
// if a specific notification
if ( isset($notification['name']) && 'Your notification name' == $notification['name'] ) {
// determine the parent post by calling get_post_meta on the child post
$parent_id = get_post_meta($post_id, '_wpcf_belongs_parentposttype_id', true);
// get all the email addresses from the parent post
$parent_field_values = get_post_meta($parent_id, 'wpcf-email-field', false);
// now $parent_field_values holds an array of all the email addresses in the parent post.
foreach($parent_field_values as $email) {
// work your php magic here to append each email address to the recipients array based on the example in the docs
}
// return the updated array of recipients
return $recipients;
}
}
You'll change "Your notification name" to match your actual notification name, and change "wpcf-email-field" to match the custom repeating email field slug. The "wpcf-" prefix is required here, since it's stored this way in the database. Then in the foreach loop you will follow the example to append a new email address into the recipients array.
More info about querying post meta:
https://developer.wordpress.org/reference/functions/get_post_meta/
I was following along until I got to the comment "work your php magic here to append each email address to the recipients array based on the example in the docs"
I really do not want to learn PHP. It's the number one reason that I use Toolset and why I stopped using ACF.
I think the CRED notification settings should be able to do this. It looks like it should but just doesn't work.
Not to be cranky but it looks like the notification settings really need some love. You can use shortcodes in some places but not others. There's a spelling mistake (Set from details) and the spelling for Email isn't consistent (E-mail, email, mail, etc).
In the overhaul it looks like it needs, perhaps using an email field from a parent post could be added or the ability to use the standard format for multiple addresses could be fixed/added.
I think the CRED notification settings should be able to do this. It looks like it should but just doesn't work.
Hmm, I don't see anything in the format information that references shortcodes. The accepted format for additional recipients is shown in the attached screenshot. Unfortunately the notification recipients field is not set up to accept shortcodes right now. The only way I know to do it is with the notifications API and some PHP.
or the ability to use the standard format for multiple addresses could be fixed/added.
Sorry, I'm not clear what you mean here. Can you explain what you mean by standard format, and how that would integrate with a shortcode and field solution?
I think some of my confusion comes from the subject and body fields accepting both shortcodes and subject/body codes (not sure why these are even there). I assumed that I could do the same with recipients and didn't really think to test something that I thought was a no brainer before spending a day and a half working on a project that wouldn't work.
The standard format for sending email to multiple recipients is to separate the addresses with a semi-colon. Checking the box to send a notification to a specific email address and using the standard format (without any shortcodes) for more than one address will result in no emails being sent. This was my fallback plan but it doesn't seem to work either 🙁
CRED implements wp_mail() to send notifications. In WordPress wp_mail(), the standard format for multiple email addresses is to separate addresses with commas, not semicolons:
https://developer.wordpress.org/reference/functions/wp_mail/#valid-address-formats
If you use commas instead of semicolons you can send to multiple addresses entered in the Additional Recipients field.