Navigation überspringen

[Gelöst] Multi-Select Notifcations in CRED

This support ticket is created vor 5 Jahren. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 32 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 4 Jahren, 11 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1401699

I would like to allow users to select one or more 'users' to notify when a form is 'updated' or 'submitted'. These 'users' may or may not be WP users. What I'm imagining is a checkboxes towards the bottom of the form next to the names of the persons who can be added. That way users cannot make errors with email addresses. Once a selection is made (the persons selected to receive updates on a form) those settings are preserved and messages are sent each time the form is updated unless those settings are modified.

I thought maybe I could do this by creating a field group for notifications but I can't figure out how to make that work. Essentially I need to do two things:

1 - be able to maintain a list of (DB of) notifiable users that may or may not be WordPress users
2 - make that list accessible to multiple Custom Post types(s) forms' notifications.

Thanks!

#1402289

Hello, there's nothing built-in to Forms that will allow you to extend the recipient selection inputs for Email notifications in the Form editor screen. However, there is an API that allows you to programmatically modify the recipients of an email notification. In theory, you could establish a proxy post type that represents Recipients (who may or may not be WordPress Users) and contains their email address in a custom field. Set up another custom post type that can be used to create post relationships between these Recipients and another custom post type like "List Recipients." Each Email Notification has a unique numeric ID, so you could set a custom field in the List Recipients post with that notification ID. Each Email Notification would have one corresponding List Recipient post. Now you have a way to use Toolset's built-in post relationships to connect Recipients and notifications. Let your Users build a notification list from these Recipient addresses using Views and post relationship Form(s). Then you would have to use custom code with the Forms API to query those related posts and set the proper notification recipients whenever the corresponding notification is triggered.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

#1403209

Hi Christian,

So, if I'm reading this correctly, and I feel like I'm not ;)...

I can use the following function to allow fields like 'name' to trigger a message? Or would i have to do something else?

Thanks!

/**
 * Customise CRED notification recipients by adding a BCC 
 * to the the notification "Content submitted"
 */
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Content submitted' == $notification['name'] ) {
 
        // Add a BCC to log@emailaddress.com
        $recipients[] = array(
            'to'        =>  'bcc',
            'address'   => 'log@emailaddress.com',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
          
    return $recipients;
}
#1404783
triggers.png
notification-name.png

I can use the following function to allow fields like 'name' to trigger a message?
No, what I'm talking about doesn't have anything to do with notification triggers. The trigger for the email notification will be whatever you selected in the Form's email notification settings (see triggers.png). The recipients list, however, will be modified from whatever you set in the email notification settings.

I think you were looking at this code:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
  // Check notification name matches target notification
  if ( isset($notification['name']) && 'Content submitted' == $notification['name'] ) {

Any time an email notification is triggered, the modify_recipients function you see here will be executed, because it's set up as a filter on the cred_notifications_recipient hook. The problem is sometimes you have many different email notifications, and you don't want to trigger this code for every notification. The code above is designed to solve that problem with a conditional. It compares the name of the notification that has been triggered against the name "Content submitted". See notification-name.png. The code inside the "if" conditional should only be executed if the Notification name is "Content submitted". As you can see in my screenshot, my notification name is "custom-notification-name". It's not a match, so the code inside the "if" statement would be skipped in this case.

#1404833

Function causes:

The site is experiencing technical difficulties. Please check your site admin email inbox for instructions.

#1407101

There's nothing wrong with this function, I copied and pasted it into a custom theme without any problems:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
  
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Content submitted' == $notification['name'] ) {
  
        // Add a BCC to log@emailaddress.com
        $recipients[] = array(
            'to'        =>  'bcc',
            'address'   => 'log@emailaddress.com',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
           
    return $recipients;
}

I'll be glad to take a closer look if you provide login credentials and let me know where you added the code.

#1413551

Are you saying the site crashed when Form notifications were triggered, or simply adding the code crashed the site? If it's the latter, I am not able to replicate that. I have added the code in functions.php and updated that file on the server using the File Manager plugin. I kept the old functions.php file as functions.php.bak, in case you need to revert quickly. The site isn't crashing for me right now, can you check and let me know how to replicate the crash?

#1413563

Yes, I was saying that when I copy/pasted your code before (and I tried it several times) it crashed the site. Now it's fine.

#1414855

Hmm, okay maybe you copied the partial code from this comment:
https://toolset.com/forums/topic/multi-select-notifcations-in-cred/#post-1404783

That code was incomplete, and pasting it into functions.php would have caused the crash you mentioned. Regardless, it looks like you can move forward now.

#1415049
#1415415

Ok. so should I continue from https://toolset.com/forums/topic/multi-select-notifcations-in-cred/#post-1404783?
I'm not sure, did you have follow up questions about the general ideas here: https://toolset.com/forums/topic/multi-select-notifcations-in-cred/#post-1402289
...or were you able to start creating the post types, relationships, fields and forms introduced there?

#1416689

🙂 You had to know I would.

A) So I would create a new CPT called 'recipients' with the fields 'name', 'to', & 'email'. I add the filter to funtions, create the relationships between the new CPT and the other CPTS. create form relationships (somehow)???

B) Could I also just add a recipient field to the form and allow the user to enter email address(s) before submitting?

#1420577

A) So I would create a new CPT called 'recipients' with the fields 'name', 'to', & 'email'. I add the filter to funtions, create the relationships between the new CPT and the other CPTS. create form relationships (somehow)???
Yes, you would do all those things and more as I mentioned here:
https://toolset.com/forums/topic/multi-select-notifcations-in-cred/#post-1402289

B) Could I also just add a recipient field to the form and allow the user to enter email address(s) before submitting?
Yes, you could do that. There isn't a built-in field for including bulk addresses, so you could use a generic "multiple line" field in the Form to capture those addresses. Then you will have to use custom code to parse those addresses before using them with the cred_notifications_recipients API.

#1438551

I don't suppose you could please help with cred_notifications_recipients API? 🙂

#1441061

Yes of course, I can help with any of our APIs. What do you have in place so far and what problem do you need assistance with? Perhaps a login would be helpful so I can see what you have implemented in the Form.