Skip Navigation

[Resolved] Email notification to parent post author when child form is submitted

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

Problem:
The user would like to send the form notification to the author of the parent post.

Solution:
We can workaround this by hooking into the notification recipients filter. Check this reply https://toolset.com/forums/topic/email-notification-to-parent-post-author-when-child-form-is-submitted-2/#post-1995977

Relevant Documentation:

This support ticket is created 3 years, 1 month 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: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by JamesS2731 3 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1994371

Tell us what you are trying to do?

I have two custom post types. The video CPT where members upload their golf swings, and the Reply CPT where a golf professional posts his advice on the video. They have a one to one relationship.

I would like to be able to notify the member when a reply has been posted to their video. The Video is the parent, the Reply is the child.

Is there any documentation that you are following? I was following this - https://toolset.com/forums/topic/email-notification-to-parent-post-author-when-child-form-is-submitted/ - but wanted the email sent to the parent author email rather than an email field filled in the form.

I tried amending the code example to the following, but it hasn't worked:

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

if (179 == $form_id){

$parent_post_id = toolset_get_related_post($post_id, 'video' );
$author_id = get_post_field ('post_author', $parent_post_id);
$post_email = get_the_author_meta( 'user_email', $author_id );

$recipients[] = array(
'to'=>'to',
'address'=>$post_email,
'name'=>'',
'lastname'=>''
);
}
return $recipients;
}

I'm also unsure of where to add the email messages, etc. (just in the email notification section of the form itself?)

Is there a similar example that we can see? - Not that I can find.

What is the link to your site? - hidden link

Any thoughts or ideas much appreciated.

Kind regards
James

#1994441

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

From first sight, I would say that you did not pass the correct relationship slug, or an array with the parent and the child post type to the second argument of toolset_get_related_post.
It should be something like:

$parent_post_id = toolset_get_related_post($post_id, array( 'video', 'reply' ) );

Or:

$parent_post_id = toolset_get_related_post($post_id, 'video-reply' );

depending on the Reply CPT slug, or the relationship slug. Check the documentation of the function https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

On the other hand, I would suggest a solution that does not need any custom code. Using a generic field, and Toolset views shortcode.
1. Add a generic number field to the form, and hide it using CSS. Add the parent post author ID to its value using the wpv-post-author

[wpv-post-author format="meta" meta="ID" item="@video-reply.parent"]

2. Configure the notification to be sent to a User with the ID coming from a generic field. And choose the generic field that you have created.

Read more about the forms notification and Toolset shortcodes below:
- https://toolset.com/course-lesson/send-notifications-when-someone-submits-the-form/
- https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-154504

I hope this helps. Let me know if you have any questions.

#1994687
Screenshot 2021-03-20 at 21.29.11.png
Screenshot 2021-03-20 at 21.28.45.png
Screenshot 2021-03-20 at 21.17.20.png

Hi Jamal,

Many thanks for the quick reply.

Your method sounds a lot easier! Ok, I've added a generic hidden field, Added your snippet as follows:

[wpv-post-author format="meta" meta="ID" item="@reply-to-video.parent"]

reply-to-video is the relationship slug.

The email works, but it gets sent to the person creating the reply form, rather than the author of the video post.

I've also tried:

[wpv-post-author format="meta" meta="ID" item="@video.parent"]

video is the Video CPT (parent) slug

and

[wpv-post-author format="meta" meta="ID" item="@replies.parent"]

replies is the Reply CPT (child) slug.

I've attached screengrabs of the hidden generic field, the email notification and the form itself just in case I've missed anything.

Is there anything you can think of that I should check?

Best regards
James

#1995107

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Based on the screenshot, I think that you have set everything correctly. Would it be possible to let me log in to your website as an administrator and test it? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

If I couldn't find out what the cause of the issue, I might need to take a copy of your website for further debugging. Let me know if that's fine with you!

#1995781

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Unfortunately, the log-in URL does not work for me, it redirects me to https://toolset.com/?login=failed I assume that you have a security plugin that hides the WordPress admin area. Please update the credentials with the working URL.

Please add the following details:
- What form are you working on?
- Where can we see this form in the frontend?

#1995977

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

After checking the form, I am afraid, my suggestion will not work. The form is created on a separate page and holds a field to choose the parent post(Video). My suggestion would be relevant if the form was added inside the Video content template instead of a separate page.

This brings us again to the solution that you wanted to implement, changing the notification recipients using custom code. After checking the relationship, I would suggest the following code

add_filter('cred_notification_recipients', 'video_reply_notification', 10, 4);
function video_reply_notification ($recipients, $notification, $form_id, $post_id){
	if (179 == $form_id){
		$parent_post_id = toolset_get_related_post( $post_id, 'reply-to-video' );
		$author_id = get_post_field( 'post_author', $parent_post_id );
		$post_email = get_the_author_meta( 'user_email', $author_id );
		
		$recipients[] = array(
			'to'=>'to',
			'address'=>$post_email,
			'name'=>'',
			'lastname'=>''
		);
	}
	return $recipients;
}
#1995979

Hi Jamal, many thanks for looking into this.

Ok, thats great. That gives me two options. I'll try the code first and if I have any issues, I'll just pop the reply form into the video content template.

Many thanks as always!

Best regards
James

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