Skip Navigation

[Resolved] How to conditionally send form notifications

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

Problem:
A Form includes a generic field, and the form notifications should be sent depending on the value of this generic field. How is that possible?

Solution:
The solution involves the use of the Toolset Forms API and some custom code.

Specifically, two hooks are used, cred_notification_event_type and cred_custom_notification_event_type_condition.

The first registers a custom type of event (distinct from the standard event types such as on submit), and the second performs some logic for the custom event and returns true or false depending on whether the notification should be sent or not.

Here is some boilerplate code that needs expanding to include such logic:

function tssupp_custom_notification($notification_type, $notification, $form_id, $post_id) {
 
    if (
        // Target the form and its notification
        $form_id == 39 && $notification['name'] == 'Thing published'
    ) {
        // Define a custom notification type
        $notification_type = 'maybe_notify';
    }
    return $notification_type;
}
add_filter('cred_notification_event_type', 'tssupp_custom_notification', 99, 4);
 
function tssupp_custom_notification_condition($condition_status, $notification, $form_id, $post_id) {
    if (
        // Target the form and notification and the notification type
        $form_id == 39 && $notification['name'] == 'Thing published' && $notification['event']['type'] == 'maybe_notify'
    ) {
        // Perform whatever tests you want and return true (send notification) or false (don't)
 
        $condition_status = true;
    }
    return $condition_status;
}
add_filter('cred_custom_notification_event_type_condition', 'tssupp_custom_notification_condition', 99, 4);

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

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 12 replies, has 3 voices.

Last updated by markR-11 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1148919
send-email-conditional.png

Tell us what you are trying to do? I have a CRED Form for updating a post. When the administrator 'approves' or 'denies' the post through this CRED form, they have the option send a notification email or not to the post author (the member who owns the post). I want that radio button for sending an email notification to be based two generic fields used in the CRED form: send-email-notification and approval-deny. That way I can have two notification emails, set, one for if they want the email sent and it was approved, and the other if they want the email notification sent, and it was denied.

Unfortunately when I do this, and choose 'When custom fields are modified' as the notification email setting, the field list is empty, so I cannot make it conditional on this.

Is there any documentation that you are following? Nope

Is there a similar example that we can see?

What is the link to your site? hidden link

#1149129

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

I'm looking into this, not sure of a solution at the moment.

#1149226

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm trying to get this working with the cred_notification_event_type API filter, but it's not behaving as expected.

I'm still on it, and will get back to you.

#1149238

Thank you Nigel!

Cheers,
Mark

#1149249

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

So the good news is that the cred_notification_event_type filter is not working (I'm not mad!), which is also the bad news.

I don't see any other way to achieve what you want without it, so I'm waiting on a fix from the developers to be able to resume.

I'm escalating this, and will keep you posted.

#1149264

Great, thank you Nigel!

#1149907

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The devs have a fix for the broken filter, which will be included in the next Forms update (expected next week).

I'm marking this as "Fixed in next release" which means it will be on my radar when the update is published, and which will then allow me to propose and test a solution.

#1158797

Hey Nigel,

Which release are we expecting a resolution for this?

Cheers,
Mark

#1158801

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Just checking, it should have been fixed in the recent Forms 2.2 release.

I'll have to take a look to remind myself where we are up to, give me a little while.

#1158823

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so I tested and confirmed this works.

What is needed from you is the logic about whether the custom field values should trigger the notification or not, which I'll leave to you.

Note that in the API filters you have the post id of the new post that you can use to retrieve custom fields, but if you are using generic fields you will probably want to retrieve them from the $_POST object.

Here is some sample code to get your started:

function tssupp_custom_notification($notification_type, $notification, $form_id, $post_id) {

	if (
		// Target the form and its notification
		$form_id == 39 && $notification['name'] == 'Thing published'
	) {
		// Define a custom notification type
		$notification_type = 'maybe_notify';
	}
	return $notification_type;
}
add_filter('cred_notification_event_type', 'tssupp_custom_notification', 99, 4);

function tssupp_custom_notification_condition($condition_status, $notification, $form_id, $post_id) {
	if (
		// Target the form and notification and the notification type
		$form_id == 39 && $notification['name'] == 'Thing published' && $notification['event']['type'] == 'maybe_notify'
	) {
		// Perform whatever tests you want and return true (send notification) or false (don't)

		$condition_status = true;
	}
	return $condition_status;
}
add_filter('cred_custom_notification_event_type_condition', 'tssupp_custom_notification_condition', 99, 4);

Note that we use two API filters. The first allows us to create a custom notification type (rather than on submit, for example). In my example code the first filter is used to create a custom type of "maybe_notify", but only for the form and notification in question.

The second filter, for the same form and notification and also testing for our new custom notification type, let's you return true or false according to whether the notification should be sent or not.

In my example I'm simply returning true. This is where you will need to insert some logic to check your field values and decide whether to send the notification or not.

I'm off (already) for the rest of the week, but if you get stuck let me know and I'll help when I'm back, or a colleague might be able to.

#1160056

Nigel,

Thank you! This worked, and the logic was simple to implement in my place.

Perhaps a feature to not require this custom code might be a nice add?

Regardless, thank you as always!

Cheers,
Mark

New threads created by Waqar and linked to this one are listed below:

https://toolset.com/forums/topic/split-ability-to-control-cred-form-notifications-based-on-generic-field/

#1160488

Hi Mark,

Thanks for the update and glad Nigel's suggestion proved useful.

He is away until next week, but you're welcome to mark this ticket as resolved and open a new one for a different question/concern.

As for the feature request, I've split it as a new ticket and we'll follow up on it accordingly.
( https://toolset.com/forums/topic/split-ability-to-control-cred-form-notifications-based-on-generic-field/ )

regards,
Waqar

#1160489

Great, thank you!