Home › Toolset Professional Support › [Resolved] Email notification to parents post authors on many to many relationship
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)
Related documentation:
This topic contains 30 replies, has 2 voices.
Last updated by Minesh 4 years, 6 months ago.
Assisted by: Minesh.
Hi, I have a many to many relationship between two post types: companies and attributes.
A company could have many attributes and an attribute could belong to many companies.
I've created a view that displays all the companies for a specific attribute (every attribute has a page).
I've create another post type called message and place a message post form in each single attribute page (where the companies that have that attribute are shown through the view).
Now, I'd like to send an email notification to all the companies (to the authors email) that have a specific attribute, when a new message post are created with the message post form displayed on the attribute page.
Is it possible? How can I do it?
Hello. Thank you for contacting the Toolset support.
Toolset Form's offers hook called cred_notification_recipients to add as many notification recipients as you want once you have all post author email address for your post type companies.
More info:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
You should try to get company post author emails based on the current attribute ID and try to find all related company 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, once you fetch the correct related company posts based on current attribute ID, you should add the emails to cred_notification_recipients hook.
Please check the following kind of related ticket that might help you
=> https://toolset.com/forums/topic/send-email-notification-to-all-child-posts-usersauthors/#post-1279475
You need to adjust the code as per your relationship and as per your requirement.
Thank you Minesh.
I've tried, but is not working.
This is the function I've created:
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4); function modify_recipients($recipients, $notification, $form_id, $post_id) { if (11040== $form_id){ // here I placed my form ID $parent_post_id = $post_id; $relationship_slug = 'relationship-slug'; // here I placed the original relationship slug $child_posts = toolset_get_related_posts( $parent_post_id, $relationship_slug, 'parent', 1000000, 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; }
I'm not sure if the code here is right, because I'm on the child post and I'd like to send the notification email to its parents.
$child_posts = toolset_get_related_posts( $parent_post_id, $relationship_slug, 'parent', 1000000, 0, array(), 'post_object', 'child' );
I will require access details to know your post-relationship slug and please share the link of the page where I can see your form.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Thank you - Can you please confirm where you added the Toolset form - on what page?
Hi, the post form ID is 11040 (Form di contatto generico CER).
I've added it on the template ID 3494 (Template for Codici CER).
You can fill the form in this page: hidden link
Ok that you for sharing all required details.
So, as I understand, I can see the form with this link: hidden link
And once I click on submit button, it should send the email to post author's of the post that is listed just besides the form (Sicily and test) - correct? If yes:
- Can you please share where you added the cred_notification_recipients hook code?
Yes, exactly what I need 🙂
I've added the cred_notification_recipients hook code on the file functions.php here: /wp-content/themes/Avada-Child-Theme
It seems there is issue with the plugin Post SMTP configuration.
When I try to send an test email from email notification configuration - on email log it says invalid username and password:
- hidden link
Can you please make emails working on your install or I am missing something?
Hi, I was looking at the logs to understand if the notifications were generated.
by the way, I've fixed it. Now it's working fine.
Still I can see the notification hook we added "cred_notification_recipients" is not get triggered.
Toolset forms plugin is using wordpress function wp_mail() to send the email notifications:
=> https://developer.wordpress.org/reference/functions/wp_mail/
I wonder why the email notification is still not triggered when you submit the form.
Can you please try to use the following sample code:
function tssupp_custom_form_recipients( $recipients, $notification, $form_id, $post_id ){ echo "<pre>"; echo "Hook cred_notification_recipients successfully triggeted."; exit; if ( 11040== $form_id ){ echo "<pre>"; print_r($recipients); exit; } return $recipients; } add_filter( 'cred_notification_recipients', 'tssupp_custom_form_recipients', 10, 4);
Can you please use above code and make sure that when you submit the form the above hook should be triggeted and it should print "Hook cred_notification_recipients successfully triggeted" on the page.
Hi, I've added it, but it doesn't print the sentence.
This is really strange.
Can you please try to add the following code - add the following code to functions.php file and remove all other cred_notification_recipients hooks.
function cred_notification_recipients_timestamp( $arg ){ error_log("cred_notification_recipients: this is triggered successfully at time " . microtime() ); return $arg; } add_filter( 'cred_notification_recipients', 'cred_notification_recipients_timestamp',10,4 );
Do you see the error log is printed with your error log file?
The first thing is that we need to make sure that the hook "cred_notification_recipients" is triggered when you submit the form.
If this does not work, I suggest you check on another test server without SMTP plugin and check email is working or not.
If that does not help, I need to create a test site to see if there is issue from our plugin.
I'll try on a new clean test site.
Ok fine - please do not forget to enable error or debug logging so that the error_log should be written to your error log file:
More info:
=> https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#php-debugging