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.
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.
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;
}
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.
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?
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.
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.
I tested, but still don't receive any notification per e-mail.
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.
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.