Skip Navigation

[Resolved] Notification on field change

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Waqar 1 year, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2637109
Untitled-1.jpg

I'm not quite sure if I understand how email notifications work when custom fields are modified or whether it just doesn't work. No documentation that I could find.

Under the setting
When custom fields are modified
I have Additional field conditions: the field selected
Only if field value has changed CHECKED

When I have the operator as <> then I get a notification every time a field is modified on the user. The field I want to get a notification about is also on the user and it's a WYSIWYG field. I expect it to only notify me when something in the field actually changed.
When I set the operator to = then I get nothing, not even when I modify the field.

#2637433

Hi,

Thank you for contacting us and I'd be happy to assist.

> When I set the operator to = then I get nothing, not even when I modify the field.

- This sounds expected because when the operator in the condition is set to ' = ', with the value field set to empty, the condition will become true when:

a). The changed/new value in the target field is empty

And

b). Because of the checked 'Only if field value has changed' option, it is also important that previously the value was not empty

> When I have the operator as <> then I get a notification every time a field is modified on the user. The field I want to get a notification about is also on the user and it's a WYSIWYG field. I expect it to only notify me when something in the field actually changed.

- I'm afraid, I couldn't fully understand these statements. Can you please explain these a little more, perhaps with an example?

regards,
Waqar

#2637439

We have User
the user has custom fields
A
B
C

On a form I have field B

That field has a value, the user doesn't change it, I still get a notification

When I go to the form that has field C and submit it, I get a notification even though the notification is on another form

I just want to get an email when field B changes from empty to anything or form something back to empty.

#2638597

Thank you for sharing these details.

For these very specific requirements, you'll need to add some code customization.

On my test website, I was able to achieve this using the following steps:

1. In my user edit form (ID: 156), I included a WYSIWYG type field with slug 'user-desc-2'.

2. In the same form, I also included a 'hidden' type generic field with slug 'user-desc-2-current'.

For this field's default value field, I used the shortcode that gets the current value of the WYSIWYG 'user-desc-2' field.
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#wysiwyg )


[types usermeta='user-desc-2' user_current='true' suppress_filters='true' output='raw'][/types]

This hidden field will help in comparing the values of the field, before and after the form has been submitted.

3. As a last step, I used a custom function attached to the 'cred_notification_recipients' hook.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients )

This function will make sure that the target form's specific notification ( in this case 'Test notification' ), is only sent if, before and after values of the field are different and one of them is empty:


add_filter('cred_notification_recipients', 'modify_recipients_empty_field_check', 10, 4);
function modify_recipients_empty_field_check($recipients, $notification, $form_id, $user_id) {

	// Check notification name and form ID matches target notification name and form ID
	if ( ( isset($notification['name']) && 'Test notification' == $notification['name'] ) && ( $form_id == 156 ) ) {
		// if current and previous values for the target field are different and one of them is empty, send notification
		if( ($_POST['wpcf-user-desc-2'] != $_POST['user-desc-2-current']) && ((empty($_POST['wpcf-user-desc-2'])) || (empty($_POST['user-desc-2-current']))) )
		{
			return $recipients;
		}
	}
	else {
		return $recipients;
	}

}

Note: You'll need to update the form ID, form notification name and the field slugs used in this code snippet to match the ones from your website.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2640369

Thanks but I'd like to use the native function of Toolset, what exactly is the "Only if field value has changed" checkbox for?

#2640549

> what exactly is the "Only if field value has changed" checkbox for?

- When this checkbox is checked, the form checks whether the submitted value of the target field is different from what it was when the edit form was initially loaded (i.e. before submission or editing).

But your requirement is two-fold, as it needs to check, whether the new value or the previous value was empty (also excluding the case when both are empty).

So in this case, the native functions won't be enough and you'll have to make use of the custom code, that I referred to earlier.

#2640867

Let's forget about the empty part of the issue.

Any field change on the User triggers the email alert. If you can get it working with just that one field, great, if it works as you said, I'm happy with that. But it doesn't work like that.
Just changing another field on the User that's in another form triggers the alert.

#2640927

> Just changing another field on the User that's in another form triggers the alert.

- In my tests on my website, I couldn't reproduce this behavior.

Can you please share the temporary admin login details and the exact steps to see this happening? I'll be able to troubleshoot this accordingly.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2640981

I think that I just realized the issue. If we look at what it says in the form
Notification e-mail trigger
- When submitting the form with payment details
- When payment is complete
- When a user is updated by this form
- When custom fields are modified

The last one says FIELDS which is plural, so I assume it means that it looks at anything user-related. I figured it was just confined to the field in the form. If you can confirm this is the case, then we can close this.

Now, I'm hoping that the option "When a user is updated by this form" will only respond to the one form as it says "this".

#2641229

> The last one says FIELDS which is plural, so I assume it means that it looks at anything user-related. I figured it was just confined to the field in the form. If you can confirm this is the case, then we can close this.

- 'When custom fields are modified' option detects changes to the user fields globally, not just limited to the forms. However, the specific condition under this option can only be set for the fields that are available in the current form.

> Now, I'm hoping that the option "When a user is updated by this form" will only respond to the one form as it says "this".

- Yes, that is correct.