Skip Navigation

[Resolved] Sending email to the grandparent post email field

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

Problem:

1) I am using below post type structures
Grandparent post type "Agents"
- Parent post type "Property"
-- Child post type "Property Inquiries"

2) Use CRED form to create the child "Property Inquiries" post, and I want to send mail to the "Agents" email address "agent-email-address".

Solution:

You can use CRED filter hook "cred_notification_recipients" to send the mail, for example, add PHP codes in 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 (2215 == $form_id){
        $agent_slug = "agents";
        $property_slug = "properties";
        $email_field_slug = "agent-email-address";
         
        $property_id_slug = '_wpcf_belongs_' . $property_slug . '_id';
        $agent_id_slug = '_wpcf_belongs_' . $agent_slug . '_id';
         
        $property_id = get_post_meta($post_id, $property_id_slug, true);
        $agent_id = get_post_meta($property_id, $agent_id_slug, true);
        $post_email = get_post_meta($agent_id, 'wpcf-' . $email_field_slug, true);
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Relevant Documentation:

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

This support ticket is created 6 years, 8 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 Luo Yang 6 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#623132

What I would like to do for my current project ( real estate site ) is the following:
I have type: Property . A property is the child of the type: Agent it belongs to. So far so good.

On my type: Property details page ( single ) I display agent information already, including the headshot and name of the agent etc. You can see that here on my dev site:

hidden link

Below that you can see there is a cred form for a user to express interest in the current property.
When a user fills out this form, I want to send a notification to the agent of the given property. What I need to do is get the agent-email-address from the parent and stick it inside my cred form as the value of one of the inputs.

I used this technique to create an agent contact form on the type:Agent detail ( single ) and I assign the value of agent-email-address in the form so that I can send a notification to the agent there, but getting the "parent property" does not seem to work.

I also noticed that any other parent fields displayed elsewhere on the page perfectly do not work inside the cred form. My thought is that is a shortcode inside a shortcode and that is why it wont work.. Is there anyway to do what I need to do here?

Thank you!

#623178

Hello,

You can use CRED filter hook "cred_notification_recipients" to send the mail to parent "agents" email address, for example, add PHP codes in 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 (2215 == $form_id){
        $parent_slug = "agents";
        $email_field_slug = "agent-email-address";
        $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-' . $email_field_slug, true);
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Please replace 2215 with your CRED form ID
replace "agents" with the parent "agents" post type slug
replace "agent-email-address" with the email field slug

#623505

Hi Luo,

I tried out your code this evening. For some reason Im still unable to get this to work. My form only sends the notification to the recipient I supply, It is not adding the agent.

I verified the information is correct as well in terms of the slug, and email field slug. Strangely the form id you used in your example was the correct value as well, 2215.

Are there any options or settings I need on the notification to make this work? I added just myself as a recipient there so that i could pass validation and save the notification.

#623516

Above PHP codes work in my localhost, since it is a PHP codes problem, please provide a test site with same problem, and fill below private detail box with login details and FTP access, also point out the problem page URL and CRED form URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks

#623533

Thanks for the details, here are what I found in your website:
1) You are using below post type structures
Grandparent post type "Agents"
- Parent post type "Property"
-- Child post type "Property Inquiries"

2) Use CRED form to create the child "Property Inquiries" post, and you want to send mail to the "Agents" email address
So it's sending email to the grandparent post email field, it is different from this ticket title:
Use Parent Custom Fields in Cred Form

I have modified 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 (2215 == $form_id){
        $agent_slug = "agents";
        $property_slug = "properties";
        $email_field_slug = "agent-email-address";
		
        $property_id_slug = '_wpcf_belongs_' . $property_slug . '_id';
        $agent_id_slug = '_wpcf_belongs_' . $agent_slug . '_id';
		
		$property_id = get_post_meta($post_id, $property_id_slug, true);
		$agent_id = get_post_meta($property_id, $agent_id_slug, true);
		$post_email = get_post_meta($agent_id, 'wpcf-' . $email_field_slug, true);
        $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$post_email );
    }
    return $recipients;
}

Please test again, check if it is fixed or not, thanks

#623689

Luo,

Your solution is simple and elegant, it works perfectly. The method you used to traverse the objects with the _wpcf belongs will help me with other issues I was having in another site as well. Im not sure why I didn't think of that...

I somehow didn't realize that this email was actually being added from the context of the inquiry itself and not the page it lives on, Im glad you pointed that out.

This is the second time I've needed to use support, and both times, it is fantastic and extremely efficient. Thank you for being great.

#624109

You are welcome