Skip Navigation

[Resolved] CRED-notification to email from parent post field

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

Problem: I would like to send a CRED notification to an email address that is stored in a custom field on the CRED post's parent post. How can I access that parent post field in the CRED notification recipients configurations?

Solution:
The most flexible, preferred method today is to use the CRED API hook cred_notification_recipients to modify the "to", "cc", or "bcc" of the notification email.

Within the callback, you can figure out the parent post by calling get_post_meta on the child post, and accessing a hidden field that relates a parent post ID to the child post. The name of that field follows the format "_wpcf_belongs_" + parent post type slug + "_id". So if your parent post type slug is "event", then the postmeta key is "_wpcf_belongs_event_id" and the code is:

$parent_id = get_post_meta($post_id, '_wpcf_belongs_event_id', true);

Once you know the parent post ID, use it to call

get_post_meta

again on the parent post to retrieve the email field value. Types fields use the prefix "wpcf-" in the postmeta table, so if your email field slug is "email-address-of-organizer" then the code would look like this:

$email_of_organizer = get_post_meta($parent_id, 'wpcf-email-address-of-organizer', true);

Now the variable $email_of_organizer contains the email address from the parent post. Use that to modify the code sample on the API documentation page to fit your needs.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
Click the orange "+More" button to see a code sample.

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.

Our next available supporter will start replying to tickets in about 6.40 hours from now. Thank you for your understanding.

This topic contains 14 replies, has 2 voices.

Last updated by thomasD-2 3 years, 3 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#601782

I need to follow-up on a topic we discussed 3 years ago:
https://toolset.com/forums/topic/send-cred-notification-to-email-from-parent-post-custom-field/

At that time the problem seemed to be solved. But the feature didn't go to the production environment, as it wasn't needed anymore. Now the feature is an requirement again, and I tried to test and implement the solution we achieved 3 years ago. However I don't get it to work again.

Is there something that has changed in the past years? What is the current and recommended way of implementing this feature today.

The requirement - briefly explained - is the following:
- There is a (parent) post type "event" that contains the custom field "email address of organizer"
- There is a child post type "registration". The CRED form for this post type shall collect the data of the registrant.
- After submitting the CRED form shall send two notifications by email: a) to the "email address of organizer" (as defined in the parent post) and b) to the email address of the registrant as collected in the CRED form.

My problem: The notification to the organizer doesn't work. I didn't manage to include the organizer's email address correctly in the CRED form.

#601860

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi,
The most flexible, preferred method today is to use the CRED API hook cred_notification_recipients to modify the "to", "cc", or "bcc" of the notification email:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
Click the orange "+More" button to see a code sample.

Within the callback, you can figure out the parent post by calling get_post_meta on the child post, and accessing a hidden field that relates a parent post ID to the child post. The name of that field follows the format "_wpcf_belongs_" + parent post type slug + "_id". So if your parent post type slug is "event", then the postmeta key is "_wpcf_belongs_event_id" and the code is:

$parent_id = get_post_meta($post_id, '_wpcf_belongs_event_id', true);

Once you know the parent post ID, use it to call get_post_meta again on the parent post to retrieve the email field value. Types fields use the prefix "wpcf-" in the postmeta table, so if your email field slug is "email-address-of-organizer" then the code would look like this:

$email_of_organizer = get_post_meta($parent_id, 'wpcf-email-address-of-organizer', true);

Now the variable $email_of_organizer contains the email address from the parent post. Use that to modify the code sample on the API documentation page to fit your needs. Let me know if you have additional questions about this API hook and how to implement it.

#602086

Hi Christian, thx for the quick response.
For the time being I'm not competent in coding PHP and WP hooks (and that's the reason why WP Toolset is an interesting option for me). Maybe I will dive deeper into the hooks option later, but for the time being I would prefer a simple solution via shortcodes or something ile that.
Is the a more shortcode-like solution?

#602089

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

CRED notification recipients fields are not configured to accept shortcodes, unfortunately. The only way to send a notification to a dynamic email address stored in a parent post's custom field requires the CRED API and PHP as I described above. I can help you set this up if you'd like some assistance with the code. Let me know how you would like to proceed.

#602890

Yes, some assistance with the code would be helpful and much appreciated. In the documentation on the website I'm missing some introduction for API-beginners.
I need to add the code to the functions.php of my child theme, right? Do I have to add anything to the CRED form, too?

#602999

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

No need to add anything special to the CRED form. If you're using the example from the cred_notification_recipients documentation, just add the code to your child theme's functions.php file and modify the notification name "Content submitted" to match your notification name (from the notifications editor in your CRED form). If you are not using a child theme, be aware that any updates to your theme in the future will overwrite your changes to functions.php - so always use a child theme to add code.

#604043

Thx, understood. However, I have tried to bring the PHP code together and added it to the functions.php, but it didn't work. No email was sent.
Could you please send me the entire code I have to add to the functions.php (of course I will be able to replace the slugs by the correct slug names of my data model). Thnak you.

#604128

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Please copy + paste the code that you used, and we can troubleshoot it together.

#604130

Here we go:

 
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']) && 'Mail an Veranstalter' == $notification['name'] ) {
        // Add email recipient
		$parent_id = get_post_meta($post_id, '_wpcf_belongs_veranstaltung_id', true);
		$email_of_organizer = get_post_meta($parent_id, 'wpcf-veranstalter-email', true);
        $recipients[] = array(
            'to'        =>  'cc',
            'address'   => '$email_of_organizer',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
    return $recipients;
#604149

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay thanks. Please add error log statements to your code so we can see more detail about each step in the process.

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
     
function modify_recipients($recipients, $notification, $form_id, $post_id) {
error_log('modify recipients called');
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Mail an Veranstalter' == $notification['name'] ) {
error_log('inside if');
        // Add email recipient
        $parent_id = get_post_meta($post_id, '_wpcf_belongs_veranstaltung_id', true);
error_log('parent: ' . $parent_id);
        $email_of_organizer = get_post_meta($parent_id, 'wpcf-veranstalter-email', true);
error_log('email of organizer: ' . $email_of_organizer);
        $recipients[] = array(
            'to'        =>  'cc',
            'address'   => '$email_of_organizer',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
error_log(print_r($recipients, true));
    return $recipients;
}

Then turn on server logs and trigger the email notification with CRED. If you're not familiar with server logs, I can show you how to activate them. 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 CRED form to trigger the email notification. If the notification is fired, this will 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.

#604412

Hi Christian,
this is the output of the error log:

[08-Jan-2018 14:23:50 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 66
[08-Jan-2018 14:23:50 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 68
[08-Jan-2018 14:24:13 UTC] PHP Notice:  Undefined index: parent_id_noncename in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/functions.php on line 34
[08-Jan-2018 14:24:13 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 66
[08-Jan-2018 14:24:13 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 68
[08-Jan-2018 14:31:53 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 66
[08-Jan-2018 14:31:53 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 68
[08-Jan-2018 14:32:26 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 66
[08-Jan-2018 14:32:26 UTC] PHP Notice:  Undefined variable: aria_req in /home/forge/dev.baum-und-bogen.de/public/wordpress/wp-content/themes/baylys/comments.php on line 68

I checked the error re. "parent_id_noncename" in themes/baylys/functions.php on line 34, which is the parent theme, and I found the following code:

 
//Add the meta box callback function
function admin_init(){
add_meta_box("veranstaltung_parent_id", "Veranstaltung Parent ID", "set_veranstaltung_parent_id", "veranstaltung", "normal", "low");
}
add_action("admin_init", "admin_init");

//Meta box for setting the parent ID
function set_veranstaltung_parent_id() {
  global $post;
  $custom = get_post_custom($post->ID);
  $parent_id = $custom['parent_id'][0];
  ?>
  <p>Please specify the ID of the page or post to be a parent to this Case Study.</p>
  <p>Leave blank for no hierarchy.  Case studies will appear from the server root with no assocaited parent page or post.</p>
  <input type="text" id="parent_id" name="parent_id" value="<?php echo $post->post_parent; ?>" />
  <?php
  // create a custom nonce for submit verification later
  echo '<input type="hidden" name="parent_id_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}

// Save the meta data
function save_veranstaltung_parent_id($post_id) {
  global $post;

  // make sure data came from our meta box
  if (!wp_verify_nonce($_POST['parent_id_noncename'],__FILE__)) return $post_id;
    if(isset($_POST['parent_id']) && ($_POST['post_type'] == "veranstaltung")) {
      $data = $_POST['parent_id'];
      update_post_meta($post_id, 'parent_id', $data);
    }
}
add_action("save_post", "save_veranstaltung_parent_id");

This seems to be a hack that the Toolset support has added 3 years ago when they had ftp access to the site? I personally have never edited the parent theme.

#604489

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Let's disregard the notices for now. I do not think they are relevant to the CRED problem, because the CRED notifications you just added are not being triggered. None of the log statements you added in your child theme's functions.php file were written to the logs. That indicates a problem with your CRED form's notification settings, or the functions.php file was not uploaded to your server correctly, or you are using a 3rd-party plugin that handles emails and overrides CRED's functionality.
- Please disable all non-Toolset plugins and test again. If the notification triggers logs, you know that there's a conflict with one of your plugins. If you're using a plugin that handles your emails through SMTP, this is likely the source of the problem. Reactivate your plugins one by one and test until the conflict is revealed.
- If disabling other plugins did not resolve the issue, please take screenshots showing the entire CRED form editor, and be sure to expand all the email notification sections so I can see how they are configured.
- Ensure the functions.php file was uploaded to your server or test environment in the correct location.

Let me know what you find out and we can go from there.

#605398

I deactivated all plugins, indeed there seemed to be a conflict with one of those plugins.
Please find the error log below.

[11-Jan-2018 11:22:28 UTC] modify recipients called
[11-Jan-2018 11:22:28 UTC] Array
(
    [0] => Array
        (
            [to] => to
            [address] => td@m4plus.com
            [name] => Thomas
            [lastname] => Dominikowski
        )

)

[11-Jan-2018 11:22:28 UTC] modify recipients called
[11-Jan-2018 11:22:28 UTC] inside if
[11-Jan-2018 11:22:28 UTC] parent: 430363
[11-Jan-2018 11:22:28 UTC] email of organizer: v1test@m4plus.com
[11-Jan-2018 11:22:28 UTC] Array
(
    [0] => Array
        (
            [address] => festerveranstalter@m4plus.com
            [to] => 
            [name] => 
            [lastname] => 
        )

    [1] => Array
        (
            [to] => cc
            [address] => $email_of_organizer
            [name] => 
            [lastname] => 
        )

)
#605422

Hey Christian, okay, after checking the code twice I found the error in the code. The problem was the single quotes around the variable $email_of_organizer in the array. Now everything works fine.
By the way: I have activated all the plugins again, and the error logs still work fine. Strange.
However: thank you for your patience an for your support.

#914809

Hi Folks,
this hack that we implemented a few months ago doesn't work anymore after Updating to Types 3.0 and migration of the post relationships. It's likely due to the '_wpcf_belongs_event_id' in the code

Do you have a suggestion how to fix this?
Thx.
Best
Thomas