Startseite › Toolset Professional Support › [Gelöst] Send Notification via "cred_notification_recipients to different child of parent
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.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
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)
Verwandte Dokumentation:
Dieses Thema enthält 18 Antworten, hat 3 Stimmen.
Zuletzt aktualisiert von Minesh vor 5 Jahren, 6 Monaten.
Assistiert von: Minesh.
Tell us what you are trying to do?
Maybe it is kind of complicated to explain (pic attached):
1. I have a parent post type called "company" from author A.
2. Company has a first child called "follow" from author B"
3. and the company has a second child called "jobs" from author A"
Now my Question is:
I need to send a mail to the "follow" (child) authors email address (author B) everytime the parent (company) posts a new job.
Task:
Author B should be notified when the company he is following posts a new job.
But the function i need has to check two things:
Send the email only if:
there is a relationship between the company and the like post types
and
if a custom field with fieldname: "get notification" has the value "yes".
only when these two points are true, the email should be sent.
I found a function here from Niles:
https://toolset.com/forums/topic/notification-to-child-post-authors-when-parent-post-author-edits-the-parent-post/#post-612960
/** * 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-slug'; // parent post type slug $child_slug = 'child-slug'; // 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_slug, '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' => '' ); } } return $recipients; }
But here i some help to fit it my needs!
Hope you can help me with that.
Thank you so much.
Hello,
Yes, it is possible with custom codes using cred_notification_recipients.
And the tread you mentioned above is outdated, in the latest version of Toolset plugin with new post type relationships, you will need to use new relationships API function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
For example, setup a Toolset form for creating job post:
1) when user submit the form, send an email to job post's author:
https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/
2) use filter hook cred_notification_recipients to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
In this PHP function, do these:
a) Get the parent "company" post ID:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
b) Use "company" post ID to get related "follow" post IDs
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post_types
c) use "follow" post IDs to make a loop:
check each "follow" post's field "get notification" has the value "yes", then add the "follow" post's author email address into the result.
For your reference.
Hey Luo,
thanks for your super fast reply.
So i am a non coder and i have no clue how to begin or built this. Could you help me in more detail to understand the different api and the use of it?
Cheers.
I suggest you check the steps and documents I mentioned above first, if you still need more assistance for it, please provide a test site(fresh wordpress installation without other plugins) with the same problem, also point out the problem page URL and form URL, I can try to setup a demo for you. thanks
Thanks for the details, I have edit your post to hide the credentials, and will update here if there is anything found.
There is another issue in your website, the filter hook cred_notification_recipients can not be triggered when I submit the form "Job erstellen", please provide the FTP access in below private message box, I need to do more test and debug, thanks
Thanks for the details, I have modified the PHP codes as below:
<?php /** * New custom code snippet. */ toolset_snippet_security_check() or die( 'Direct access is not allowed' ); /** * 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( 'follower notification' ); // which notifications this applies to $child_slug = 'follow'; // child post type slug if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) { // Get the parent company id of this job post $parent_id = toolset_get_related_post( $post_id, 'einrichtung-job', 'parent' ); // Check notification name matches target notification // Get the child follower posts of that parent, excluding this post (i.e. the siblings) if(!$parent_id){ return; } $args = array( 'post_type' => $child_slug, 'numberposts' => -1, 'toolset_relationships' => array( 'role' => 'child', 'related_to' => $parent_id, 'relationship' => 'einrichtung-kita-like' ), 'meta_query' => array( 'key' => 'wpcf-get-notification', 'value' => 1, 'compare' => '=' ) ); $query = new WP_Query( $args ); $child_posts = $query->posts; // Start with an empty list of email addresses and empty list of post author IDs $emails = ""; // Loop over each child post and get the post authors foreach ($child_posts as $child_post) { $author = $child_post->post_author; $emails .= get_the_author_meta( "user_email", $author ) . ','; } // Add recipients by BCC if ( $emails != '' ) { $recipients[] = array( 'to' => 'bcc', 'address' => $emails, 'name' => '', 'lastname' => '' ); } } return $recipients; }
Please test again, check if it is fixed, thanks
Hey Luo,
thanks for your help.
i tried to test it but the email is only send to the post author of the parent.
but it should send it the child follower author when:
1. a follow relation is set to a company (company-follow)
2. a job was postet which has a relation to the followed company (company-job)
and
3. the wpcf-get-notification is set to yes
these are the advantages.
hope we can fix this?
cheers
Yes, above codes should be able to do what you want.
But you will need to setup the tests, like this:
1) user A has a company post "Company A"
2) Login as user B, create a "follow" post, enable option "get-notification", related it with above company post "Company A"
3) Login as user A, create a "job" post, related it with above company post "Company A", then both users will get the email notifications.
Hey Luo,
i have set up my site new.
the domain is still the same like i posted to you in the hidden message.
Login:
toolset@test.com
pw:
ollUKh4so
therefore i can´t get it running correct. only the job author gets the email notification.
the only thing i have done, was change the follow post type to kita-like.
here is my code:
<?php /** * New custom code snippet. */ toolset_snippet_security_check() or die( 'Direct access is not allowed' ); /** * Customise CRED notification recipients by adding a BCC * to the the notification "Notification name" */ add_filter('cred_notification_recipients', 'follower_custom_recipients', 10, 4); function follower_custom_recipients( $recipients, $notification, $form_id, $post_id ) { // Edit these as required $which_notifications = array( 'follower notification' ); // which notifications this applies to $child_slug = 'kita-like'; // child post type slug if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) { // Get the parent company id of this job post $parent_id = toolset_get_related_post( $post_id, 'einrichtung-job', 'parent' ); // Check notification name matches target notification // Get the child follower posts of that parent, excluding this post (i.e. the siblings) if(!$parent_id){ return; } $args = array( 'post_type' => $child_slug, 'numberposts' => -1, 'toolset_relationships' => array( 'role' => 'child', 'related_to' => $parent_id, 'relationship' => 'einrichtung-kita-like' ), 'meta_query' => array( 'key' => 'wpcf-get-notification', 'value' => 1, 'compare' => '=' ) ); $query = new WP_Query( $args ); $child_posts = $query->posts; // Start with an empty list of email addresses and empty list of post author IDs $emails = ""; // Loop over each child post and get the post authors foreach ($child_posts as $child_post) { $author = $child_post->post_author; $emails .= get_the_author_meta( "user_email", $author ) . ','; } // Add recipients by BCC if ( $emails != '' ) { $recipients[] = array( 'to' => 'bcc', 'address' => $emails, 'name' => '', 'lastname' => '' ); } } return $recipients; }
sorry but i don´t know what i have done wrong.
Thanks for the details, I am checking it in your website again, will update here if there is anything found.
Here are what I found, you are right, the email does send to job post author only.
In order to debug this issue, I have tried these in your website
1) install plugin "WP Mail Logging" in your website,
2) create a user "luo" using my own email address,
3) use this new user to create a new "kita-like" post,
4) switch to admin user "toolset" to create a new job post,
I can see the email has been attached with BCC to my own email, you can see it here:
versteckter Link
in the first item, click "view", it shows:
Headers:
Content-Type: text/html,\nBcc: MY-EMAIL@ADDRESS,
Then I have checked again the document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
Don't find any problem in the PHP codes.
So there should be something wrong in Toolset Form plugin or something changed, I need to check it with our developers, will update here if there is anything news.
I have tried the same codes in another website, it works fine. I can get the email without problem.
There should be a compatibility/server problem in your website, so I have tried to log into your website, but the credentials you provided above is not valid any more, I get these error:
ERROR: Incorrect username or password.
https://toolset.com/forums/topic/send-notification-via-cred_notification_recipients-to-different-child-of-parent/#post-1233772
Please check it.
And please check if there there is any PHP errors in your website, and share it.
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
Thanks for the confirmation, I have change your post to private message to hide the website credentials.
Above custom PHP codes I provided above is only an example, I suggest you try to customize the PHP codes according to your own request.
According to our support policy we don't provide custom codes support:
https://toolset.com/toolset-support-policy/
For other new questions:
1) one last thing: i need the mail only be sent to the author of the child (kita-like) no on both - the author of the job post should not get the mail.
There isn't such kind of built-in feature within Toolset post form, as a workaround, you can add the parent "company" post author email in the recipient list, for example:
... // Start with an empty list of email addresses and empty list of post author IDs $emails = ""; // Get the parent company id of this job post $parent_id = toolset_get_related_post( $post_id, 'einrichtung-job', 'parent' ); // send the email to parent company post author $parent_author_id = get_post_field ('post_author', $parent_id); $emails .= get_the_author_meta ('user_email', $parent_author_id); ...
More help:
https://developer.wordpress.org/reference/functions/get_the_author_meta/
2) and in the mail-address bar is another problem. please see the pic attached.
I do't see the attached pic, please check your post:
https://toolset.com/forums/topic/send-notification-via-cred_notification_recipients-to-different-child-of-parent/page/2/#post-1236363
3) the mail is also send when the custom field "wpcf-get-notification" has not the value = 1.
It works fine in my localhost, and please check this part of PHP codes:
... 'meta_query' => array( 'key' => 'wpcf-get-notification', 'value' => 1, 'compare' => '=' ) ...
It will return posts with "wpcf-get-notification" has the value = 1.
Unless there is other compatibility/server problem in your website, please try it in a fresh wordpress installation.
More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hey Luo,
i will check these for me.
1. Allright i will change it to fit my needs.
2. Maybe 1.) is the solution for this problem.
Thank you so much for your help. Your great 🙂
Number three is still a problem.
3. this is the only problem i did not get solved. could you check my site again. There must be an error when it works on your local site.