Skip Navigation

[Resuelto] I want to use a Types field shortcode to supply multiple email addresses for CRED notifications

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I have a CRED form with email notifications. I would like to use a repeating email address field on a parent post to store all the email addresses. I would like to insert that field shortcode into the notifications recipients field so I can send a notification to all the email addresses in the parent post's custom field.

Solution: The recipient fields here do not support shortcodes at this time. The best solution is to use the CRED API cred_notification_recipients to programmatically modify the recipients and use BCC and CC effectively.

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

This support ticket is created hace 6 años, 11 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por Christian Cox hace 6 años, 11 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#600335

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.

#600346

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.

#600355

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.

#600358

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/

#600372

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.

#600495
Screen Shot 2017-12-20 at 8.22.21 AM.png

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?

#602288

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 🙁

#602350

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.