Skip Navigation

[Resolved] email address from Parent post as notification email on child form submission

This support ticket is created 6 years, 6 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by samB-3 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#581357

Tell us what you are trying to do?
I have an ads type site. When someone posts an ad there's an option to reply to them.
When a reply is sent I want CRED to send the notification to the email address from the parent post which is stored in the custom field "post-email". I would like to use a shortcode under "Where to send this notification:" but that does not work nor can I figure out how to get the parent "post-email" field to show up under "Send notification to a WordPress user specified in a form field:"

So I've added the following to my form HTML which adds the parent "post-email" field to "Send notification to a WordPress user specified in a form field:" and allows me to select it: [cred_field field="post-email" post="messages" value="[parent-info field="post-email"]" urlparam="" readonly="true"]. This works fine but anyone can view the form source code and see the email address which is not secure.

Is there any documentation that you are following?
I've tried reading all the documentation on CRED email notifications and I've also followed the advice given in the following posts and edited as directed but it does not work.

https://toolset.com/forums/topic/cred-notification-to-parent-post-author-2/
https://toolset.com/forums/topic/notification-of-author-of-a-parent-post-when-a-child-post-is-created/

I'm probably not understanding something so would really appreciate any help I can get.

What is the link to your site?
Unfortunately it is a members only site and I can't get permission from the owner to give it out to non-members.

Some info:
- Email address of person posting ad is stored in custom field "post-email"
- CRED form ID is "38"

Please let me know what other info you might need. Thank you very much.

#581429

Dear Sam,

I assume you are asking for custom PHP codes solution of CRED filter hook "cred_notification_recipients", you can try to add below codes into your theme/functions.php:


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 (38 == $form_id){
        $parent_slug = "company-profile";
		$field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
		
		$parent_post_id = get_post_meta($post_id, $field_slug, true);
		$post_email = get_post_meta($parent_post_id, 'wpcf-post-email', true);
		
		$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Please replace "company-profile" with the parent post type slug.

#581711

Thank you, that worked and I now realize that when I was copying the other examples I used the post field slug instead of the post type slug.

However, while this feature is now working I suddenly can't use %%POST_PARENT_TITLE%% in the subject or body anymore as it just comes up blank. I was able to replace "%%POST_PARENT_TITLE%%" in the notification body with "[wpv-post-link id="$post"]" but neither of them work in the notification subject.

Any idea why this would stop working just because I added that code to functions.php?

And, another question: Is there a way to grab the Name and Email entered into the form and use them as the from name and email?
Post type slug: messages
Post fields slugs: message-name, message-email

Thank you!

#581791

Q1) Any idea why this would stop working just because I added that code to functions.php?
The PHP codes I provided above only works for adding a email recipient, it should not take effect on the email notification subject, you can confirm it by this:
Remove above custom PHP codes from your theme/functions.php, and test again. if the problem persists, please create a new thread for the new question, that will help other users to find the answers.

Q2) And, another question: Is there a way to grab the Name and Email entered into the form and use them as the from name and email?
I assume you are using parent post type "messages", there are custom fields in post type "messages":
Post fields slugs: message-name, message-email
You can modify the PHP codes as below:

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 (38 == $form_id){
        $parent_slug = "messages";
        $field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
         
        $parent_post_id = get_post_meta($post_id, $field_slug, true);
        $post_email = get_post_meta($parent_post_id, 'wpcf-post-email', true);
        $message_name = get_post_meta($parent_post_id, 'wpcf-message-name', true);
         
        $recipients[] = array('to'=>'to', 'name'=>$message_name, 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}
#582108
Capture.PNG

Thanks for the help!

Q1: After testing like you suggested, I found that %%POST_PARENT_TITLE%% does not work in the notification subject line. It works with an old test site I have running CRED 1.6 but does not work two other sites one which runs CRED 1.8 and the other CRED 1.92. I will make a new post about this.

Q2: I can't get this to work but I have a feeling it's not what I needed anyway as I wasn't really clear in what I was asking. The following code does exactly what I need in that it sends a notification message from a child form to the email address stored in the parent post:

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 (38 == $form_id){
        $parent_slug = "post";
        $field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
         
        $parent_post_id = get_post_meta($post_id, $field_slug, true);
        $post_email = get_post_meta($parent_post_id, 'wpcf-post-email', true);
         
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Now what I'd like to do is also set the "From Address" and "From Name" from fields in the current child form. Is that possible?

I think these are the necessary details:

TO: Email address from Parent
- parent post type: post
- parent email field: post-email

FROM: Email and Name from current child form
- child post type: messages
- child email field: message-email
- child name field: message-name

Attached image may help explain if the above is not clear.

Thank you so much!

#582211

For the new question:
Now what I'd like to do is also set the "From Address" and "From Name" from fields in the current child form. Is that possible?

I assume we are talking about this case, you are using CRED form for creating a child post, you are going to send the notification to emails address from parent post + emails address from child post
If it is, above codes should be able to resolve the first part: send the notification to emails address from parent post
For the second part: "emails address from child post", you should be able to setup it within CRED form UI, see our document:
Automated Email Notifications With CRED
https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/

Since it is a new question, if you still need assistance for it, please create a new thread for it, that will help other users to find the answers.

#582434

Thank you for all your help Luo. I'll create new threads for the other questions.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.