Skip Navigation

[Resolved] cred_notification_recipients using parent post email field

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

Problem: I have a Form that creates child posts. I would like to use automatic email notifications to send a message to an email address field in the parent post, but I can't get the cred_notification_recipients API to use the correct email address.

Solution: Use the post relationships API and get_post_meta to access the parent post email address field:

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
// Add email recipient
$parent_id = toolset_get_related_post(
  $post_id,
  'opiskelijan-harjoittelupaikka-hakija'
);
$emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
$recipients[] = array(
'to' => 'to',
'address' => $emailpalautusosoite,
'name' => '',
'lastname' => '');
}
return $recipients;
}

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

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

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)

This topic contains 7 replies, has 2 voices.

Last updated by katjaL 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1188863

I want to send notification of child post creation in frontend to the custom field email address (palautusosoite) of the parent post (opiskelijapaikka). There are couple of threads and documentation I've followed and achieved this code below, but it doesn't send the notification. I have sent test mail with the post form notification basic test system and it works ok, but with this code, the post is not sent anywhere. How should I fix it? Thanks!

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
// Add email recipient
$parent_id = get_post_meta($post_id, '_wpcf_belongs_opiskelijapaikka_id', true);
$emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
$recipients[] = array(
'to' => 'to',
'address' => $emailpalautusosoite,
'name' => '',
'lastname' => '');
return $recipients;
}
}

#1189167

Hi, I see a couple of potential problems here.

1. The cred_notification_recipients callback function should always return an array of recipients. In your case, the "return $recipients;" line is enclosed in the "if" statement. Since nothing is returned outside the "if", no recipients will be included in any other notifications! You should probably move the return statement out of the "if" to fix this.

2. The _wpcf_belongs_slug_id syntax for accessing parent post information is deprecated in the new relationships system. So if your post relationships were created in the new system, or after migration to the new post relationship system, the old syntax may not work. You may need to use the post relationships API to get parent post information. Check our documentation for toolset_get_parent_post_by_type: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type

If neither of these seems to fix the problem, the best way to troubleshoot is to turn on server logs and use error_log to write out debug information. If you're not familiar with logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Then in your notification callback, add some error_log statements to see the code flow:

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
    error_log('inside the if');
    // Add email recipient
    $parent_id = get_post_meta($post_id, '_wpcf_belongs_opiskelijapaikka_id', true);
    error_log('parent id: ' . $parent_id);
    $emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
    error_log('email: ' . $emailpalautusosoite );
...your code continues...

When the notification is triggered, this will create an error_log.txt file in your site's root directory. You can see the results written to the logs, and determine if the problem is in the conditional, or in the parent information, or something else. I will be glad to help review those logs. Then you can revert the changes to wp-config.php

#1189186

At the moment the php code looks like the code below. Also the error log is now on, it recorded this:

[22-Jan-2019 18:48:19 UTC] inside the if
[22-Jan-2019 18:48:19 UTC] parent id: 0
[22-Jan-2019 18:48:19 UTC] email:

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
error_log('inside the if');
// Add email recipient
$parent_id = toolset_get_parent_post_by_type($post_id, 'opiskelijapaikka', true);
error_log('parent id: ' . $parent_id);
$emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
error_log('email: ' . $emailpalautusosoite );
$recipients[] = array(
'to' => 'to',
'address' => $emailpalautusosoite,
'name' => '',
'lastname' => '');
}
return $recipients;
error_log('return: ' . $recipients );
}

#1189237

Okay thank you for the logs. Since parent id = 0, we know the problem is here:

$parent_id = toolset_get_parent_post_by_type($post_id, 'opiskelijapaikka', true);

There should only be two arguments to this function, so you should delete "true". Then add some more logs to get more information about $post_id:

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
error_log('inside the if');
// Add email recipient
error_log('post id: ' . $post_id );
error_log('post type: ' . get_post_type($post_id) );
$parent_id = toolset_get_parent_post_by_type($post_id, 'opiskelijapaikka');
error_log('parent id: ' . $parent_id);
$emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
error_log('email: ' . $emailpalautusosoite );
$recipients[] = array(
'to' => 'to',
'address' => $emailpalautusosoite,
'name' => '',
'lastname' => '');
}
return $recipients;
error_log('return: ' . $recipients );
}
#1189494

Ok, I changed the code and now error log gives this (the parent id is BTW 7677):

[23-Jan-2019 03:02:09 UTC] inside the if
[23-Jan-2019 03:02:09 UTC] post id: 7703
[23-Jan-2019 03:02:09 UTC] post type: hakija
[23-Jan-2019 03:02:09 UTC] parent id: 0
[23-Jan-2019 03:02:09 UTC] email:

#1189735

May I log in and see how your post relationships are configured? Please provide login credentials in the private reply fields here.

#1189884

Okay I see that this post relationship was actually created in the new system and not migrated:
Opiskelijoiden harjoittelupaikat Hakijat
I misunderstood this, sorry. Instead of the toolset_get_parent_post_by_type API, use the toolset_get_related_post API:

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']) && 'Www-opiskelijahaku' == $notification['name'] ) {
error_log('inside the if');
// Add email recipient
error_log('post id: ' . $post_id );
error_log('post type: ' . get_post_type($post_id) );
$parent_id = toolset_get_related_post(
  $post_id,
  'opiskelijan-harjoittelupaikka-hakija'
);
error_log('parent id: ' . $parent_id);
$emailpalautusosoite = get_post_meta($parent_id, 'wpcf-palautusosoite', true);
error_log('email: ' . $emailpalautusosoite );
$recipients[] = array(
'to' => 'to',
'address' => $emailpalautusosoite,
'name' => '',
'lastname' => '');
}
return $recipients;
error_log('return: ' . $recipients );
}

Try that and let me know the results.

#1189890

Again perfect result – thank you!