Skip Navigation

[Resolved] Send notification to recipient email defined in parent post custom field

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

Problem: I have a Form that creates child posts. When the child post is published, I would like to send an automatic email notification to an email address specified in the parent post's custom fields, but I cannot specify that field in the notification settings.

Solution: You can use some custom PHP code with the cred_notification_recipients API and the toolset_get_related_post API to query the parent post, get the custom field value, and add that email address to the recipients list programmatically:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
    
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
if ( isset($notification['name']) && 'your-notification-name' == $notification['name'] ) {
// Add email recipient
$parent_id = toolset_get_related_post(
  $post_id,
  'your-relationship-slug'
);
$emailemailangehorige = get_post_meta($parent_id, 'wpcf-your-email-field-slug', true);
$recipients[] = array(
'to' => 'to',
'address' => $emailemailangehorige,
'name' => '',
'lastname' => '');
}
return $recipients;
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

This support ticket is created 3 years, 12 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
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)

Author
Posts
#1848687

When submitting a Form, I would like to send an automatic notification email to the email address defined in a custom field in the parent post.

#1848713

Hello, it seems we were disconnected in chat. I have found another ticket that discusses a similar problem, including a code sample you can use to programmatically set an email address from a parent post's custom field value: https://toolset.com/forums/topic/cred_notification_recipients-help-needed/

You can add the customized snippet in your child theme's functions.php file, or to a new custom code snippet in Toolset > Settings > Custom Code. Please let me know if you need assistance customizing this custom code snippet to work on your site.

#1853087

I'm sure I'm not using the right values in right places. Could you please check it?

Infos:
parent post slug:
gedenkseite

child post slug:
kerze

e-mail field slug of the parent post:
emailangehorige

Do you need any other slugs? Relationships? Relationship Forms? Post Forms?

Code:

 
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
   
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
if ( isset($notification['name']) && 'nachricht-angehoerige' == $notification['name'] ) {
// Add email recipient
$parent_id = toolset_get_related_post(
  $post_id,
  'gedenkseite'
);
$emailemailangehorige = get_post_meta($parent_id, 'wpcf-emailangehorige', true);
$recipients[] = array(
'to' => 'to',
'address' => $emailemailangehorige,
'name' => '',
'lastname' => '');
}
return $recipients;
}
#1853445

Hi, I see one potential problem here:

$parent_id = toolset_get_related_post(
  $post_id,
  'gedenkseite'
);

The second argument should be the slug of the post relationship, not the slug of the parent post type. You can find the relationship slug in Toolset > Relationships. Edit the gedenkseite - kerze relationship and copy the relationship slug. Replace gedenkseite in your code with the post relationship slug to retrieve the related parent post ID.
Documentation for this API and its accepted/default arguments is available here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Let me know the results of this change and we can continue troubleshooting if necessary.

#1861121

Still don't work. I think I'm missing something else.

Can I give you Worpdress admin-name and password? Maybe that would be a faster way to find the mistake?

#1861545

Sure, please provide a login in the private reply fields and let me know where I can find your custom code. I will be glad to take a closer look.

#1862635
notification-name.png

Okay I see that the notification name field was not set for the notification (see notification-name.png). I set a unique name, nachricht-angehoerige-notification-1, for this notification and updated the notification name information in functions.php in this line:

//...
if ( isset($notification['name']) && 'nachricht-angehoerige-notification-1' == $notification['name'] ) {
//...

The rest of the code looks okay at first glance. Would you like to test it out now and see if things are working as expected? I didn't want to trigger some unwanted emails without your approval. Also be sure to update any of your local development copies of functions.php with the changes I integrated on the server file so you do not unintentionally overwrite them later.

#1862709

I tested, but still don't receive any notification per e-mail.

#1862743

Okay can you give me a bit more information?
- You mentioned that you received no email notification. Is the notification email sent ONLY to the email address included in the notification recipient settings (in the Form editor page), or is the email notification sent to neither the recipient settings email address nor the parent email field address?
- If the email notification is sent to ONLY the email address included in the notification recipients settings, that indicates a problem in the custom code. I can investigate that in more detail by testing the Form. Let me know if that is okay, and where I can find the Form on the front-end of your site (URL to page containing the Form). I'll need you to stop editing the Form in wp-admin so I can take over for a closer look.
- If the email notification is sent to neither address, this could indicate a problem in the notification trigger. Try changing the trigger to "When the form is submitted" instead of a trigger based on post status, and test once again. Let me know the results.

#1862777

Aaaa, I am so stupid. Everything works fine exactly as you described already in your previous post. This one change (notification name field was not set for the notification) was already the solution.

The problem was, that I created two different forms. But I have to have ONE FORM with TWO NOTIFICATIONS. Of course it won't work, if I don't have the right form on the front-end. Aiii, I'm stupid. Sorry for that, thank you for your help and thank you for being patient. I appreciate that, Christian 🙂

The code works perfectly.