Skip Navigation

[Resolved] Multi-Select Notifcations in CRED

This support ticket is created 4 years, 4 months 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
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)

This topic contains 32 replies, has 3 voices.

Last updated by Christian Cox 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1443339

Okay great, I got the login but didn't get more details so I'm not sure where to go from here. I can see that you have copied the example API code and pasted it in your theme's functions.php file. Without seeing any modifications to that code, I'm not able to guess what the problem is. Also I don't know which Form we are talking about. Where I can see the Form on the front-end of the site?

#1443341

Sorry, you didn't ask.

I suppose we could start with the defect post forms. But I'm talking about option 'B' here. So there isn't the need to build any CPT stuff out for messaging right?

#1444545

Okay yes, I'm talking about option B as well. As far as I understand it you are correct, no additional CPT is needed to support these notifications. To summarize, Option B is based on a Form that creates or edits Defect posts. In that Form, you must add a generic 'multiline' field where site users will enter their desired email notification recipients. When the Form is submitted, the custom code in your cred-notification-recipients hook is triggered. In that hook, you will be able to access the information from the generic multiline field and use that information to update the recipients array.

If you are unfamiliar with generic fields, we have information available for those here: https://toolset.com/documentation/user-guides/front-end-forms/inserting-generic-fields-into-forms/

In the cred-notification-recipients callback function, you will be able to access the field contents in the $_POST superglobal using the generic field slug as the key. So if your generic field slug from the form builder is "notification-recipient-emails" then you can access that field's contents in the callback as:

$_POST["notification-recipient-emails"]

Let me know if you are able to implement this or if you get stuck on something specific, and I can provide some additional feedback. It will be helpful if you share a URL where I can see this Form on the front-end of the site to review your Form modifications.

#1445581

So you used the existing multiline field in the post form when you added the hook? An example on the front end would be /defect/defect-test-2/?content-template-id=130

#1446611

So you used the existing multiline field in the post form when you added the hook?
No, I didn't add the hook to your site and I had no knowledge of any existing multiline field.

An example on the front end would be /defect/defect-test-2/?content-template-id=130
Got it, thank you! I can see the Edit Defect Form here.

Let me know if you get stuck on something specific and I can offer more guidance.

#1448863

Sorry Christian, I think we had a miscommunication. I thought you asked for credentials to add/configure the API. I'm not sure exactly what in Reply #1444545 you are directing me to do other than read on how to insert a field which I had already done. I really don't understand the callback piece. Is there something else I'm missing here?

#1448975

I think the type of help I offer is unclear, so let me try to clarify. I am able to offer guidance and help fix problems in your work, but I'm not here to do the work for you. If you have questions about the concepts I have described so far, please ask them! On the other hand, if you don't care to understand the concepts of our software features, our relevant APIs, or if you don't know how to write PHP effectively, now is the time to employ a development resource who can do those things for you because you are asking for something that is not a trivial change or enhancement. I can provide examples of using our APIs and links to documentation for those APIs to achieve some specific piece of functionality, but I cannot create new features for you or provide cut-and-paste solutions for complex problems. That's part of our support policy: https://toolset.com/toolset-support-policy/

We do offer a contractors portal here where you can connect with skilled independent contractors: https://toolset.com/contractors

For smaller projects you may also try https://codeable.io/developers/toolset

#1451159

Christian,

It wasn't my intention to imply that I wanted you to do this work on your own. And certainly I feel like I'm pushing Toolset (and wordpress) as far as it can go. The site(s) I build certainly aren't standard by any means, they tend to be more on the service/app side. Anyway...

From what I understand from your prior reply we need to first add a function that looks something like:

function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
     $myheaders = array();
     if ( $formid == 129 && $notification_name == 'Notify list' ) {
         $recipients = get_post_meta( $postid, 'wpcf-emails', true );
         $myheaders = array( 'To: ' . $recipients );
    }
     return array_merge($headers, $myheaders);
}
add_filter('cred_mail_header', 'c

Do I need a seperate function for each form that has the field or can I do it a different way? Right now it is using the ID for the edit defect post form.

Do we need to call that function in the custom code?

Did I misunderstand your instructions completely?

Thanks!

Larry

#1456267

Do we need to call that function in the custom code?
The add_filter line tells the function to fire whenever any email notification is triggered. You do not need to do anything else to call that function. The add_filter line handles that automatically.

Do I need a seperate function for each form that has the field or can I do it a different way? Right now it is using the ID for the edit defect post form.
You don't need a separate function for each form, you could modify the conditional logic in the existing function to work with multiple form IDs. For example, this conditional tests if the Form being submitted has any one of 3 different IDs:

$forms = array( 123, 234, 345 );
if( in_array( $formid, $forms ) ) {
  // this is one of the three form IDs above
}

Note that the variable $formid is one of the callback parameters, so you know which Form has triggered this code.

#1456359

Is there a way to let it work with all forms by default and then just add the 'emails' field as needed? Or will that cause errors?

#1456713

Is there a way to let it work with all forms by default and then just add the 'emails' field as needed? Or will that cause errors?
That's pretty much how I have been describing it so far. I'm not sure why you changed from the cred_notification_recipients API I originally suggested into cred_mail_headers here:
https://toolset.com/forums/topic/multi-select-notifcations-in-cred/page/2/#post-1451159

Here is the correct format and syntax for you to start with:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
   // this function is triggered any time an email notification is triggered
   $forms = array( 123, 234, 345 );
   if( in_array( $formid, $forms ) ) {
      // this is one of the three form IDs above so add your custom code here
   }
          
    return $recipients;
}

Change 123, 234, 345 to be a comma-separated list of form IDs where you want to add the email address as needed.

#1456809

I think you misundersood my question.

Both functions (the one I selected and the one you provided) have: $forms = array( 123, 234, 345 );

What I was asking is if I could eliminate or change $forms = array( 123, 234, 345 ); so that I wouldn't have to keep modifying that like everytime I add the 'emails' field (or remove it) to a post form? Or would that cause errors because it would expect that field to be there and not see it??

#1456811

What I was asking is if I could eliminate or change $forms = array( 123, 234, 345 ); so that I wouldn't have to keep modifying that like everytime I add the 'emails' field (or remove it) to a post form?
Yes of course, if you want to remove all conditional logic inside the callback function feel free to do so. Just be aware that whatever code you put in the function will be triggered for every email notification in every Form you or anyone else ever adds to the site.

Or would that cause errors because it would expect that field to be there and not see it??
Your custom code may or may not generate errors, it is impossible for me to predict.

#1462389

I added the function, as you recommended but I get the usual:

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

If i try eliminating the $forms = array it doesn't fix the error.

#1467673

Okay, what did you add, and where did you add it? This code is currently in functions.php:

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;
}

If you re-use the function name "modify_recipients" anywhere you will experience an error. This is probably what happened. Either remove the above code from functions.php, edit it directly to make your changes, or choose a new function name in your new code. Otherwise, it would make sense to share the exact code you added whenever you experience an error. I don't know what broke so I cannot provide a solution.

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