Skip Navigation

[Resolved] Conditional cred notifications

This thread is resolved. Here is a description of the problem and solution.

Problem:
What is the

cred_notification_recipients

hook and how to use it?

Solution:
Full description here:
https://toolset.com/forums/topic/conditional-cred-notifications/#post-560240

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 7 years, 3 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 12 replies, has 3 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#556726

Hello,

I am trying to setup a complex notification system, where :
- each cred form has several notifications ;
- each notification may be or not be sent to one or several users.

Conditions for choosing who will receive the notifications mainly are :
- the current user role ;
- the other user roles ;
- a custom field of the parent post.

I have found this code and tried to adapt, but I can't make it work :

add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients){
    if (!is_array($recipients)) {
        $recipients=array();
    }
    if($form_id == 505){
		$user_info = get_userdata(get_current_user_id());
		$email_utilisateur = $user_info->user_email;
		if ( current_user_can('administrator') ) {
			$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$email_utilisateur);
		}
    }
    return $recipients;
}

1) First problem is the code don't work... I edit as admin, and therefore I should receive the email, but I don't.
2) Second problem is I can't see where to specify which notification will be sent...

Thank you.

#556780

We cannot assist code that extends the Toolset functionality:
https://toolset.com/toolset-support-policy/

What you need is currently not possible with CRED.

But I filed a request just a few days ago in CRED to allow exactly that:
- Send notifications to all users of a particular role.

When we implement this, I cannot yet say.
I added your ticket, so the feature request gets more votes and hence more attention and priority.

I do not understand this your requirement, however:
- the other user roles

What do you mean with it?

We cannot query all users to check the non-relevant user's role.
Doing this would stop the performance of CRED completely.

Maybe I misunderstand, please can you elaborate on this?

Related to send it depending on a custom field of the parent post, this not possible for one reason:
- Create New Post Forms have no "real" post ID yet.
And that means such a condition would try to get a parent of a non-existent Post.

What you can do here instead, is to call the parent Field with as example a Types/Views ShortCode where you pass the Parent ID manually.
Then, with this ShortCode, you can populate any Generic Field (best, a numeric one) in the Form.

You can then use the Generic Field as a condition to your notifications.

Related to the Code, it is not a Public API hook:
cred_notification_recipients is not present here:
https://toolset.com/documentation/programmer-reference/cred-api/

Please point me to the exact thread where you learned of this hook, as it is either a "not public" hook, or we miss the DOC for it, and hence I would need to request one, and then, we will also be able to assist you more in detail with this code.

Thank you for the kind cooperation!

#558025

Please don't close this thread yet.
Thank you.

#558026

Please don't close this thread yet.
Thank you

#558077

If you require more assistance please let me know the additional information I requested earlier.

Thank you.

#558117

Hello and thank you for your precise answer.
In deed the the "send to all users with this roles" would me a great feature.

In the meanwhile, here is where I get the hook the https://toolset.com/forums/topic/notification-of-author-of-a-parent-post-when-a-child-post-is-created/#post-364785
First, I just need this code to work, so I will be able to look forward on customizing it.
Thank you.

#558118

That thread is over a Year old and I suspect that API is deprecated, as in our DOC it is not present anymore.

I need to check with our team what we know about it and will be back soon.

#558127

You can use this API hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header

1. It has a notification_name attribute; this lets you set a condition for each notification.
2. You can create an array of users to receive the email if you do not want to set that in the GUI (which is entirely possible when you use " Send notification to a particular email address:" and Separate multiple recipients with commas.)
Then you can use that Custom Array of Receivers in the "to" parameter of the list here:
hidden link
3. The parent Post Field can only be checked in Custom PHP, by using get_post_meta() and using the ID of the parent Post

Hence, this is possible with that CRED API.

Related to the other API function, I am still awaiting info.

Please let me know if you can move on with this information.

#559266

Please don't close this thread yet.
I need a bit more time to take a look at your solution and see if it fit my needs.
In the meanwhile, have your got any further information about the other API function ?
Thank you.

#559675

Hello,

Thank you for the documentation ; I managed to create my whole conditional notifications system.

Still, on one of the functions, I need to merge several headers, and tried this :

        $myheaders1 = array();
        $user_id1 = get_the_author_meta('ID');
        $currentuser = get_user_by('ID', $user_id1);
        $myheaders1 = array( 'To: '.$currentuser->display_name.' <'.$currentuser->user_email.'>' );

        $myheaders2 = array();
		$niveau_confirmation = get_post_meta($postid,'wpcf-confirmation', true);
		if ($niveau_confirmation==2) {$user_id2 = get_post_meta($postid,'wpcf-id-responsable-projet', true);}
        $respprojet = get_user_by('ID', $user_id2);
        $myheaders2 = array( 'To: '.$respprojet->display_name.' <'.$respprojet->user_email.'>' );

        $myheaders3 = array();
		$niveau_validation = get_post_meta($postid,'wpcf-validation', true);
		if ($niveau_validation==2) {$user_id3 = get_post_meta($postid,'wpcf-id-validateur', true);}
        $validateur = get_user_by('ID', $user_id3);
        $myheaders3 = array( 'To: '.$validateur->display_name.' <'.$validateur->user_email.'>' );

        $headers = array_merge($headers, $myheaders1, $myheaders2, $myheaders3);

But it seems the headers always only gets the last one of the 3 "myheaders".

Why would that be ?

Thank you.

#560058

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

Beda is off today, he will be back to work tomorrow and takecare of this ticket.

Thank you

#560240

In the meanwhile, have your got any further information about the other API function ?

Yes:
cred_notification_recipients is a filter to manage recipients while sending the Notification

the parameters are => $recipients, $notification, $form_id, $post_id

$recipients is the current recipients array to where notification will be sent and has a structure like this:

$recipients[] = array(
'address' => $address,
'to' => $to ['to','cc','bcc'],
'name' => $name,
'lastname => $lastname
); 

$notification is the notification that will be sent
$form_id is the cred form that contains that notification
$post_id is the post id is going to be created/edited by the form

Here a real example
We created a notification, and when we submit the cred form we apply this filter:

add_filter('cred_notification_recipients', 'recipients_handler', 10, 4);
function recipients_handler($recipients, $notification, $form_id, $post_id) {
         //i can add new recipient
         $recipients[] = array('to' => 'to', 'address' => 'mynewemail@email.it', 'name' => '', 'lastname' => '');
         
         return $recipients;
}

$to can be: 'to', 'cc', 'bcc'
$address is the email
$name *optional is name
$lastname * optional is lastname

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Hence your issue above is expected.
hidden link

This part of the code is Custom Code, that is not using Toolset API or variables or structures, for a more customized help on this particular issue, I suggest to consult a certified partner.
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

#560273

OK now I understand ; the "To:" was replacing the previous, so instead of merging various "To:", I've added the other addresses by separating them with comas.
It all works great now.
Thank you.