Home › Toolset Professional Support › [Resolved] Send Email Notification to all child posts users(authors)
Problem:
Send Email Notification to all child posts users(authors) with forms
Solution:
You should try to use the Toolset Form's hook cred_notification_recipients to add as many notification recipients as you want.
You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/send-email-notification-to-all-child-posts-usersauthors/#post-1279475
Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
Tagged: Content-submission forms, CRED API, Toolset Forms
Related documentation:
This topic contains 25 replies, has 2 voices.
Last updated by udais 5 years, 6 months ago.
Assisted by: Minesh.
I want to send email notification to all child posts user(authors) on their registered email id.
I have 3 post types:
1 parent
2 child
3. message
setup following one to many relations
1. parent child
2. parent message
created view parent to display all parent posts.
in this view i am also displaying all child posts. (screenshot attached) and form to send message.
How to send email notification to authors of all child posts (on their registered email id) displayed on this view when submitting message form.
see attachment attachemts.
Thanks
Hello. Thank you for contacting the Toolset support.
I would like to know here - have you created Toolset form to send the message?
Hello.
yes created form for post type: message...... and form is added in view... already sent in screen shot.
Well - you should try to use the Toolset Form's hook cred_notification_recipients to add as many notification recipients as you want.
More info:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
You should try to get prent ID and based on the parent ID try to find all child posts using the Toolset post-relationship API function: toolset_get_related_post
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Now, based on related posts (child posts) you should add the emails to cred_notification_recipients hook.
Hello
Thanks for quick reply.
I already tried but does not work.........
and tried
/**
* Customise CRED notification recipients by adding a BCC
* to the the notification "Notification name"
*/
add_filter('cred_notification_recipients', 'tssupp_custom_recipients', 10, 4);
function tssupp_custom_recipients( $recipients, $notification, $form_id, $post_id ) {
// Edit these as required
$which_notifications = array( 'Notify sibling' ); // which notifications this applies to
$parent_slug = 'parent'; // parent post type slug
$child_slug = 'child'; // child post type slug
// Check notification name matches target notification
if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {
// Get the parent id of this post
$parent_id = get_post_meta( $post_id, 'wpcf_belongs_' . $parent_slug . '_id', true );
// Get the child posts of that parent, excluding this post (i.e. the siblings)
$args = array(
'post_type' => $child,
'numberposts' => -1,
'post__not_in' => $post_id,
'meta_key' => 'wpcf_belongs_' . $parent_slug . '_id',
'meta_value' => $parent_id
);
$child_posts = get_posts( $args );
// Start with an empty list of email addresses and empty list of post author IDs
$emails = "";
$authors = array();
// Loop over each child post and get the post authors
foreach ($child_posts as $child_post) {
$authors[] = $child_post->post_author;
}
// Ignore duplicates
$authors = array_unique( $authors );
// Get email address of authors and create comma separated list
foreach ($authors as $author) {
$emails .= get_the_author_meta( "user_email", $author ) . ',';
}
// Add recipients by BCC
if ( $emails != '' ) {
$recipients[] = array(
'to' => 'bcc',
'address' => $emails,
'name' => '',
'lastname' => ''
);
}
}
Please try to use the following code:
/** * Customise CRED notification recipients by adding a BCC * to the the notification "Notification name" */ add_filter('cred_notification_recipients', 'func_send_notification_authorof_child_post', 10, 4); function func_send_notification_authorof_child_post( $recipients, $notification, $form_id, $post_id ) { // Edit these as required $which_notifications = array( 'Notify sibling' ); // which notifications this applies to // Check notification name matches target notification if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) { // Get the parent id of this post - adjust the following line of code accordingly to get dynamic parent ID $parent_id = 999; // Get the child posts of that parent, excluding this post (i.e. the siblings) $child_posts = toolset_get_related_posts( $parent_id, 'relationship-slug', // replace the original relationship slug 'parent', 999, 0, array(), 'post_object', 'child' ); // Start with an empty list of email addresses and empty list of post author IDs $emails = ""; $authors = array(); // Loop over each child post and get the post authors foreach ($child_posts as $child_post) { $authors[] = $child_post->post_author; } // Ignore duplicates $authors = array_unique( $authors ); // Get email address of authors and create comma separated list foreach ($authors as $author) { $emails .= get_the_author_meta( "user_email", $author ) . ','; } // Add recipients by BCC if ( $emails != '' ) { $recipients[] = array( 'to' => 'bcc', 'address' => $emails, 'name' => '', 'lastname' => '' ); } } }
Wheree:
- Adjust the relationship-slug as well as $parent_id values as well as the notification name
hello
I tried code sent by you but its not working.........
notification name: Notify Child Post Authors
parent post id=7
/**
* Customise CRED notification recipients by adding a BCC
* to the the notification "Notification name"
*/
add_filter('cred_notification_recipients', 'func_send_notification_authorof_child_post', 10, 4);
function func_send_notification_authorof_child_post( $recipients, $notification, $form_id, $post_id ) {
// Edit these as required
$which_notifications = array( 'Notify Child Post Authors' ); // which notifications this applies to I replaced with notification name
// Check notification name matches target notification
if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {
// Get the parent id of this post - adjust the following line of code accordingly to get dynamic parent ID
$parent_id = 7; //i replaced with parent id
// Get the child posts of that parent, excluding this post (i.e. the siblings)
$child_posts = toolset_get_related_posts(
$parent_id,
'parent-child', // replace the original relationship slug i replaced
'parent',
7, //i replaced with parent id
0,
array(),
'post_object',
'child'
);
// Start with an empty list of email addresses and empty list of post author IDs
$emails = "";
$authors = array();
// Loop over each child post and get the post authors
foreach ($child_posts as $child_post) {
$authors[] = $child_post->post_author;
}
// Ignore duplicates
$authors = array_unique( $authors );
// Get email address of authors and create comma separated list
foreach ($authors as $author) {
$emails .= get_the_author_meta( "user_email", $author ) . ',';
}
// Add recipients by BCC
if ( $emails != '' ) {
$recipients[] = array(
'to' => 'bcc',
'address' => $emails,
'name' => '',
'lastname' => ''
);
}
}
}
Can you please send me problem URL and access details so I can look at the issue and fix it for you.
I have set the next reply to private which means only you and I have access to it.
Can you please check now. I've added the code to Toolset's "Custom Code" section, that means you do not need to add addon plugin to add custom code. I've adjusted and moved the code here:
=> hidden link
/** * Customise CRED notification recipients by adding a BCC * to the the notification "Notification name" */ add_filter('cred_notification_recipients', 'modify_recipients', 10, 4); function modify_recipients($recipients, $notification, $form_id, $post_id) { //error_log(print_r($notification, true)); $which_notifications = array( 'Notify Child Post Authors' ); // which notifications this applies to // Check notification name matches target notification if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) { $parent_id = 7; $child_posts = toolset_get_related_posts( $parent_id, 'parent-child', // replace the original relationship slug 'parent', 999, 0, array(), 'post_object', 'child' ); // Start with an empty list of email addresses and empty list of post author IDs $emails = array(); $authors = array(); // Loop over each child post and get the post authors foreach ($child_posts as $child_post) { $authors[] = $child_post->post_author; } // Ignore duplicates $authors = array_unique( $authors ); // Get email address of authors and create comma separated list foreach ($authors as $author) { $emails[] = get_the_author_meta( "user_email", $author ); } //error_log(print_r($emails, true)); // Add recipients by BCC if (!empty($emails)) { $recipients[] = array( 'to' => 'bcc', 'address' => join(",",$emails), 'name' => '', 'lastname' => '' ); } //error_log("recipients---".print_r($recipients, true)); } return $recipients; }
More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
I see it's just working fine now.
Checking........
Yes - I checked with email log plugin and it's working. Can you please confirm it works at your end as well.
Hello
I checked......
mail is only receiving to one email id(first) not all.
I also checked in wp- mail log is showing \n after first email
you may check wp-mail log in wp-admin
I've added the following email log plugin and I do not see the issue:
=> hidden link
As you can see the log it do not display the \n after first email.
hidden link
is not showing \n
but you may check following url its showing \n after first email id and no email is being received after first email id. i checked it. you may check by changing users email id yourself.
hidden link
and first email id is 2 times.
The first email ID is two times because with original notification you added that email.
I've added the following line of code at top of the function:
=> hidden link
$recipients = array();
I can see now there is no duplicate email and no \n issue.