Skip Navigation

[Resolved] Notification e-mail to parent post’s author not working

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

Problem: I have added some custom code using the CRED API which should modify the notification email recipients list and include the parent post's author email address. It does not seem to be working.

Solution: Check the syntax and be sure your variables are correct. In this case the parent_post_author_id variable was not correctly identified with a $. The corrected code is:

/ Notification
add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients, $notification, $form_id, $post_id){

if (204 == $form_id){
$parent_slug = "opdracht";
$field_slug = '_wpcf_belongs_' . $parent_slug . '_id';

$parent_post_id = get_post_meta($post_id, $field_slug, true);
$parent_post_author_id = get_post_field ('post_author', $parent_post_id );
$author_email = get_the_author_meta('user_email', $parent_post_author_id );
$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$author_email );
}
return $recipients;
}

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

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

Author
Posts
#638719

I've set up a child post type called "offertes" (also known as a 'quote'). It's the child post type of my post type 'opdracht' (project in english). Now whenever a user submits a quote the author of the parent post type should receive an e-mail notification.

I've added the following code to my functions.php:

// Notification
add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients, $notification, $form_id, $post_id){

if (204 == $form_id){
$parent_slug = "opdracht";
$field_slug = '_wpcf_belongs_' . $parent_slug . '_id';

$parent_post_id = get_post_meta($post_id, $field_slug, true);
$parent_post_author_id = get_post_field ('post_author', $parent_post_id );
$author_email = get_the_author_meta('user_email', parent_post_author_id );
$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$author_email );
}
return $recipients;
}

In which the id is the form ID of the child post type. But the parent author doesn't receive an e-mail. When adding the recipient there's no way to add the parent post type author or a shortcode. Am I doing something terribly wrong here?

Kind regards

#639394

I don't see anything obviously wrong with this code. If you hard-code the author email and test, is the notification sent as expected? You can also add some log statements and turn on server logs, then monitor the logs when you submit the form as a test. Add logs like this:

add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients, $notification, $form_id, $post_id){

if (204 == $form_id){
error_log('form 204 submitted');
$parent_slug = "opdracht";
$field_slug = '_wpcf_belongs_' . $parent_slug . '_id';

$parent_post_id = get_post_meta($post_id, $field_slug, true);
error_log('parent post id: ' . $parent_post_id );
$parent_post_author_id = get_post_field ('post_author', $parent_post_id );
error_log('parent post author id: ' . $parent_post_author_id );
$author_email = get_the_author_meta('user_email', $parent_post_author_id );
error_log('author email: ' . $author_email);
$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$author_email );
}
error_log(print_r($recipients, true));
return $recipients;
}

If you're not familiar with server 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');

Submit the child post form again. This should create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php and delete the log file.

#641650
errorlog.png

Hi,

I did as you said. The error log is enclosed. Also here as text:

[10-Apr-2018 08:33:50 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:33:59 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:34:39 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:34:43 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:34:48 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:34:52 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:34:56 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:00 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:07 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:25 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:28 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:30 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:35:51 UTC] PHP Notice:  Undefined variable: link in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 61
[10-Apr-2018 08:36:13 UTC] form 204 submitted
[10-Apr-2018 08:36:13 UTC] parent post id: 148
[10-Apr-2018 08:36:13 UTC] parent post author id: 1
[10-Apr-2018 08:36:13 UTC] PHP Notice:  Use of undefined constant parent_post_author_id - assumed 'parent_post_author_id' in /home/u2330p23763/domains/klussersvinden.nl/public_html/wp-content/themes/klussersvinden/functions.php on line 118
[10-Apr-2018 08:36:13 UTC] author email: 
[10-Apr-2018 08:36:13 UTC] Array
(
    [0] => Array
        (
            [to] => to
            [name] => 
            [lastname] => 
            [address] => 
        )

)
#643547

Thank you, is the parent post ID (148) correct? If so, please change line 13 in the code I provided above to be this:

$author_email = get_the_author_meta('user_email', $parent_post_author_id );

Test once more and let me know the results.

#644906

Hi Christian, that did the trick! Thanks alot.